小fish 发表于 2015-4-23 05:07:03

Python处理Excel文档(xlrd, xlwt, xlutils)

简介

  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: (:( ,)+;)+
-- :
(加粗为默认格式,以下所列并非全部)
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

  1. The xlrd Module
  2. The xlwt Module
  3. Working with Excel files in Python
  
页: [1]
查看完整版本: Python处理Excel文档(xlrd, xlwt, xlutils)