python操作xlsx檔案的包openpyxl執行個體

來源:互聯網
上載者:User
下面為大家分享一篇python操作xlsx檔案的包openpyxl執行個體,具有很好的參考價值,希望對大家有所協助。一起過來看看吧

Python擴充庫openpyxl,可以操作07版以上的xlsx檔案。可以建立活頁簿、選擇使用中工作表、寫入儲存格資料,設定儲存格字型顏色、邊框樣式,合併儲存格,設定儲存格背景等等。

需要增加可以顏色進入包安裝目錄的

your_pthon_path/site-packages/openpyxl/styles

修改colors.py檔案下的

COLOR_INDEX = ( '00000000', '00FFFFFF', '00FF0000', '0000FF00', '000000FF', #0-4 '00FFFF00', '00FF00FF', '0000FFFF', '00000000', '00FFFFFF', #5-9 '00FF0000', '0000FF00', '000000FF', '00FFFF00', '00FF00FF', #10-14 '0000FFFF', '00800000', '00008000', '00000080', '00808000', #15-19 '00800080', '00008080', '00C0C0C0', '00808080', '009999FF', #20-24 '00993366', '00FFFFCC', '00CCFFFF', '00660066', '00FF8080', #25-29 '000066CC', '00CCCCFF', '00000080', '00FF00FF', '00FFFF00', #30-34 '0000FFFF', '00800080', '00800000', '00008080', '000000FF', #35-39 '0000CCFF', '00CCFFFF', '00CCFFCC', '00FFFF99', '0099CCFF', #40-44 '00FF99CC', '00CC99FF', '00FFCC99', '003366FF', '0033CCCC', #45-49 '0099CC00', '00FFCC00', '00FF9900', '00FF6600', '00666699', #50-54 '00969696', '00003366', '00339966', '00003300', '00333300', #55-59 '00993300', '00993366', '00333399', '00333333', 'System Foreground', 'System Background' #60-64 '00D2B48C', '0087CEFA', '0000BFFF' #自己添加的)

以00+16進位RGB顏色代碼的形式即可

自己寫的一個產生xlsx檔案的代碼:

#!/usr/bin/env python#-*- coding: utf-8 -*-import openpyxlfrom openpyxl import Workbookfrom openpyxl.styles import colors,Font,Border,Side,PatternFill,Alignmentwb = Workbook() #建立活頁簿ws = wb.active #啟用活頁簿ws.merge_cells('C3:E3') #合併儲存格ws.merge_cells('C4:E4')ws.merge_cells('C20:I20')ws.merge_cells('C21:I21')left, right, top, bottom = [Side(style='thin', color='000000')]*4 #設定儲存格邊框屬性border = Border(left=left, right=right, top=top, bottom=bottom) #設定儲存格邊框格式fill1 = PatternFill(start_color = 'FFFF00', end_color = 'FFFF00', fill_type = 'solid') #設定儲存格背景色fill2 = PatternFill(start_color = 'D2B48C', end_color = 'D2B48C', fill_type = 'solid')fill3 = PatternFill(start_color = '00BFFF', end_color = '00BFFF', fill_type = 'solid')fill4 = PatternFill(start_color = 'FF0000', end_color = 'FF0000', fill_type = 'solid')align1 = Alignment(horizontal='center', vertical='center') #設定文本對齊align2 = Alignment(horizontal='left', vertical='center')for i in range(3,22): for col in 'CDEFGHIJK': ws[col+str(i)].border = border #給每個儲存格設定相應的格式  #ws[col+str(3)].fill = fill1 #ws[col+str(i)].alignment = alignfor col in 'CDEFGHIJK': ws[col+str(3)].fill = fill1 ws[col+str(20)].fill = fill3 ws[col+str(21)].fill = fill4 for i in range(4,20): ws[col+str(i)].fill = fill2for col in 'CDEFGHIJK': ws[col+str(3)].alignment = align1for i in range(4,22): for col in 'CDE': ws[col+str(i)].alignment = align2for col in 'CDEFGHIJK': ws[col+str(3)] = 'test1' #儲存格賦值for i in range(3,22): for col in 'CDE': if i in range(5,20) and col == 'C':  pass else:  ws[col+str(i)] = 'test2'for i in range(4,20): for col in 'EFGHIJK': ws[col+str(i)] = 50for i in range(20,22): for col in 'JK': ws[col+str(i)] = 100wb.save('test.xlsx') #儲存檔案

儲存格字型等也可以使用相應的模組去設定。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.