python:基于xlrd和xlwd模块的Excel操作

基于xlrd和xlwd的Excel操作

基本概念

  • xlrd 用于读取Excel数据表格;xlwt 用于写入表格;仅支持 .xls.xlsx 格式
  • 如果Excel表的文字编码格式为 gb2312 ,读取后会显示乱码,请先转成 Unicode
  • workbook 定义为 工作薄sheet 定义为 工作表cell 定义为 单元格
  • 一个 workbook 包含多个 sheet ;一个 sheet 包含多个 cell

xlrd模块

  1. workbook 对象及方法
  • open_workbook() 函数创建(读取Excel表格)并返回
  • workbook 对象的创建及常用方法如下:
方法描述
open_workbook()读取指定的Excel文件,返回一个 workbook 对象; 该对象具有多个方法,用于操作 sheet 工作表
Book.nsheets返回 sheet 数目
Book.sheets()返回包含所有 sheet 对象的 list
Book.sheet_by_index(index)返回指定索引处的 sheet 对象
Book.sheet_names()返回包含所有 sheet 对象名称的字符串 list
Book.sheet_by_name(name)返回指定名称的 sheet 对象
  1. sheet 对象及方法
  • 基于 workbook 对象的方法获取 sheet 对象
  • sheet 对象的操作方法如下,通过 sheet 对象获取 cell 内容(字符串)
方法描述
Sheet.name返回 sheet 对象(工作表)的名称
Sheet.nrows返回 sheet 对象(工作表)的行数
Sheet.ncols返回 sheet 对象(工作表)的列数
Sheet.row(r)获取 sheet 对象(工作表)的指定行,返回包含所有 Cell 对象的 list
Sheet.col(c)获取 sheet 对象(工作表)的指定列,返回包含所有 Cell 对象的 list
Sheet.row_values(r)获取 sheet 对象(工作表)中指定行的值,返回包含所有 Cell 值的字符串 list
Sheet.col_values(c)获取 sheet 对象(工作表)中指定列的值,返回包含所有 Cell 值的字符串 list
Sheet.cell(r, c)根据位置获取 sheet 对象(工作表)中指定的Cell 对象
Sheet.cell_value(r, c)根据位置获取 sheet 对象(工作表)中指定 Cell 对象的值(字符串)
Cell.value返回单元格的值(字符串)

xlwt模块

  1. workbook 对象及方法
  • xlwt.Workbook.(encoding=‘ascii’, style_compression=0) 创建 workbook 对象,被用于保存 sheet 对象,是保存内容的首要步骤
  • 常用方法如下
方法描述
Workbook.add_sheet(sheetname, cell_overwrite_ok=False)Workbook 中创建并返回一个被添加的 Worksheet 对象
sheetname 是指被添加的 worksheet 对象的名称
cell_overwrite_ok 是指是否重写 cell 中的内容
Workbook.save(filename_or_stream)保存 workbook 到指定的 Excel 文件
  1. Worksheet 对象的方法
  • 基于 xlwt.Workbook() 方法创建了 worksheet 对象。随后,用于写入具体的内容。

  • 常用的方法为 worksheet.write() ,用于将 Cell 写入 workbook

  • 参数说明如下
    xlwt.Worksheet.write(r, c, label=’’, style=<xlwt.Style.XFStyle object>)
    r – 第几行(0-index)
    c – 第几列(0-index)
    label – 写入 Cell 的数据(内容)的格式

    • An int, long, or Decimal instance is converted to float.
    • A unicode instance is written as is. A bytes instance is converted to unicode using the encoding, which defaults to ascii, specified when the Workbook instance was created.
    • A datetime, date or time instance is converted into Excel date format (a float representing the number of days since (typically) 1899-12-31T00:00:00, under the pretence that 1900 was a leap year).
    • A bool instance will show up as TRUE or FALSE in Excel.
    • None causes the cell to be blank: no data, only formatting.
    • An xlwt.Formula instance causes an Excel formula to be written.

    style – 作用于 Cell 的格式

    • A style, also known as an XF (extended format), is an XFStyle object, which encapsulates the formatting applied to the cell and its contents.
    • XFStyle objects are best set up using the easyxf() function. They may also be set up by setting attributes in Alignment, Borders, Pattern, Font and Protection objects then setting those objects and a format string as attributes of an XFStyle object.
  1. Cell内容的格式
    基于 XF record 允许用户显式设置 Cell内容 的格式
GroupAttributes
Number formatNumber format index (index to FORMAT record)
FontFont index (index to FONT record)
AlignmentHorizontal and vertical alignment, text wrap, indentation, orientation/rotation, text direction
BorderBorder line styles and colours
BackgroundBackground area style and colours
ProtectionCell locked, formula hidden
  1. xlwt.Style.easyxf(strg_to_parse=’’, num_format_str=None, field_sep=’, ‘, line_sep=’;’, intro_sep=’:’, esc_char=’\’, debug=False)
  • 该方法创建并配置 XFStyle objects , 并用于 Worksheet.write() 方法.
  • 接受并解析 Alignment, Borders, Font, Pattern and Protection 对象.
  • Parameters: num_format_str –
    • To get the “number format string” of an existing cell whose format you want to reproduce, select the cell and click on Format/Cells/Number/Custom. Otherwise, refer to Excel help.
    • Examples: “#,##0.00”, “dd/mm/yyyy”
  • Returns: An XFstyle object.

————————————————

参考文件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值