嵌入式linux Python-excel表格操作
Xlsx是python用來構造xlsx檔案的模組,可以向excel2007+中寫text,numbers,formulas 公式以及hyperlinks超連結。
可以完成xlsx檔案的自動化構造,包括:
合併單元格,製作excel圖表等功能:
1,Introduction:
xlsxWriter支援多種excle功能;與excel完美相容;寫大檔案,速度快且只佔用很小的記憶體空間
不支援讀或者改現有的excel檔案
2, Installing:
sudo pip install XlsxWriter;
sudo easy_install XlsxWriter;
3,使用:
import xlsxwriter workbook= xlsxwriter.Workbook('hello.xlsx') # 建立檔案 worksheet = workbook.add_worksheet() # 建立sheet, 可以work.add_worksheet('employee')來指定sheet名,但中文名會報UnicodeDecodeErro的錯誤 worksheet.write('A1', 'Hello world') # 向A1寫入 workbook.close()
excel公式計算
1234567891011121314151617181920212223 | # Some data we want to write to the worksheet. expenses = ( [ 'Rent' , 1000 ], [ 'Gas' , 100 ], [ 'Food' , 300 ], [ 'Gym' , 50 ], ) # Start from the first cell. Rows and columns are zero indexed. 按標號寫入是從0開始的,按絕對位置'A1'寫入是從1開始的 row = 0 col = 0 # Iterate over the data and write it out row by row. for item, cost in (expenses): worksheet.write(row, col, item) worksheet.write(row, col + 1 , cost) row + = 1 # Write a total using a formula. worksheet.write(row, 0 , 'Total' ) worksheet.write(row, 1 , '=SUM(B1:B4)' ) # 呼叫excel的公式表示式 workbook.close() |
excel自定義格式:
import xlsxwriter # 建檔案及sheet. workbook = xlsxwriter.Workbook('Expenses02.xlsx') worksheet = workbook.add_worksheet() # Add a bold format to use to highlight cells. 設定粗體,預設是False bold = workbook.add_format({'bold': True}) # Add a number format for cells with money. 定義數字格式 money = workbook.add_format({'num_format': '$#,##0'}) # Write some data headers. 帶自定義粗體blod格式寫表頭 worksheet.write('A1', 'Item', bold) worksheet.write('B1', 'Cost', bold) # Some data we want to write to the worksheet. expenses = ( ['Rent', 1000], ['Gas', 100], ['Food', 300], ['Gym', 50], ) # Start from the first cell below the headers. row = 1 col = 0 # Iterate over the data and write it out row by row. for item, cost in (expenses): worksheet.write(row, col, item) # 帶預設格式寫入 worksheet.write(row, col + 1, cost, money) # 帶自定義money格式寫入 row += 1 # Write a total using a formula. worksheet.write(row, 0, 'Total', bold) worksheet.write(row, 1, '=SUM(B2:B5)', money) workbook.close()
excel寫入時間格式
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 | from datetime import datetime import xlsxwriter # Create a workbook and add a worksheet. workbook =
|