簡介
xlrd,xlwt和xlutils是用Python處理Excel文檔(*.xls)的高效率工具。其中,xlrd只能讀取xls,xlwt只能新建xls(不可以修改),xlutils能將xlrd.Book轉為xlwt.Workbook,從而得以在現有xls的基礎上修改數據,並創建一個新的xls,實現修改。
(以下屬性或方法並非全部,需要更多屬性請參看文檔;建議先參考文末Demo,再深入了解)
xlrd
Book(class) 由xlrd.open_work("example.xls")返回
nsheets: sheets數
sheet_names: sheet名稱列表
sheets: sheet列表
sheet_by_index(sheetx): 按序號提取sheet
sheet_by_name(sheet_name): 按名稱提取sheet
Sheet(class) 由Book object相關方法返回
name: sheet名
nrows: 行數
ncols: 列數
cell(rowx,colx): 第rows行colx列的單元格
cell_type(rowx,colx): 數據類型
cell_value(rows,colx): 數值
col(colx): 第colx列所有單元格組成的列表
col_slice(colx,start_rowx=0,end_rowx=None): 第colx列指定單元格組成的列表
col_types(colx,start_rowx=0,end_rowx=None): 第colx列指定單元格數值類型組成的列表
col_values(colx,start_rowx=0,end_rowx=None): 第colx列指定單元格數值組成的列表
row同樣有col的各項操作,此處略去
Cell(class) 由Sheet object(s)相關方法返回
ctype: 一個int型變量,對應不同的數值類型
value: 單元格的值
xlwt
Workbook(class) 由xlwt.Workbook()返回
encoding: 編碼方案
add_sheet(sheet_name): 添加sheet
get_sheet(Sheet_name): 選擇sheet
save(file_name): 保存
Worksheet(class) 由Workbook object相關方法返回
write(rows,colx,cell_value,style): 編輯單元格
row(rowx).write(colx,cell_value,style): 編輯行
flush_row_data(): 減少內存壓力,flush之前行不可再修改
col(colx),write(rows,cell_value,style): 編輯列
easyxf(function) 創建XFStyle instance,格式控制
expression syntax: (<element>:(<attribute> <value>,)+;)+
<element> - <attribute> - <value>:
(加粗為默認格式,以下所列並非全部)
font - bold - True or False
- colour - {colour}
- italic - True or False
- name - name of the font, Arial
- underline - True or False
alignment - direction - general, lr, rl
- horizontal - general, left, center, right, filled
- vertical - bottom, top, center, justified, distributed
- shrink_to_fit - True or False
bolders - left - an integer width between 0 and 13
- right - an integer width between 0 and 13
- top - an integer width between 0 and 13
- bottom - an integer width between 0 and 13
- diag - an integer width between 0 and 13
- left_colour - {colour}*, automatic colour
- right_colour - {colour}*, automatic colour
- ...
pattern - back_color - {colour}*, automatic colour
- fore_colour - {colour}*, automatic colour
- pattern - none, solid, fine_dots, sparse_dots
{colous}*: black, (dark_)(light_)blue, gold, (dark_)(light_)green, ivory, lavender,
(light_)orange, pink, (dark_)red, rose, violet, white, (dark_)(light_)yellow, ...
xlutils
copy: 將xlrd.Book轉為xlwt.Workbook
styles: 讀取xlrd.Workbook的每一個單元格的style
display: 簡單而安全地呈現xlrd讀取的數據
filter: 拆分與整合多個xls文件
margins: 查看表格稀疏程度
save: 序列化xlrd.Book,轉存為binary xls或stream
Tips
1. xlrd.open_workbook(fomatting_info=):當formatting_info=Ture,讀取workbook並保留格式
2. xlrd.open_workbook(on_demand=): 當on_demand=True,只有被要求時才將worksheet載入內存,讀取大文件時使用
3. xlwt.Worksheet.flush_row_data(): 減少內存占用,被刷新的行不能再訪問或修改,建議每1000行刷新一次(若列很多當調整)
4. xlwt.Workbook(encoding=): 選擇創建的workbook的編碼
Demo
import xlrd
import xlwt
from xlutils.copy import copy
# xlrd
book = xlrd.open_workbook("example.xls", formatting_info=True, on_demand=True)
sheet = book.sheet_by_index(0)
cell = sheet.cell(0,0)
# xlwt
workbook = xlwt.Workbook()
workbook.encoding = "utf-8" # Equals to workbook = xlwt.Workbook(encoding="utf-8")
sheet = workbook.add_sheet("Sheet1", cell_overwrite_ok=True)
style = xlwt.easyxf(
"font: name Arial;"
"pattern: pattern solid, fore_colour red;"
)
sheet.write(0, 0, "content of this cell", style)
sheet.row(0).set_style(style)
workbook.save("example.xls")
#xlutils
workbook = copy(book)
Reference
3. Working with Excel files in Python