Openpyxl is a Python library for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files.
打开查看Excel如下:
There is no need to create a file on the filesystem to get started with openpyxl. Just import the Workbook class and start using it.
from openpyxl import Workbook wb = Workbook()
A workbook至少创建一个worksheet.
通过openpyxl.workbook.Workbook.active()得到worksheet.ws = wb.active注意:该方法使用_active_sheet_index属性, 默认会设置0,也就是第一个worksheet。除非手动修改,否则使用active方法得到都是第一个worksheet。你也可以创建worksheets,通过 openpyxl.workbook.Workbook.create_sheet() 方法:
>>> ws1 = wb.create_sheet("Mysheet") #插入到最后(default) #或者 >>> ws2 = wb.create_sheet("Mysheet", 0) #插入到最开始的位置创建的sheet的名称会自动创建,按照sheet,sheet1,sheet2自动增长,通过title属性可以修改其名称。ws.title = "New Title"默认的sheet的tab是白色的,可以通过 RRGGBB颜色来修改sheet_properties.tabColor属性从而修改sheet tab按钮的颜色:ws.sheet_properties.tabColor = "1072BA"当你设置了sheet的名称,可以将其看成workbook中的一个key。也可以使用openpyxl.workbook.Workbook.get_sheet_by_name() 方法
>>> ws3 = wb["New Title"] >>> ws4 = wb.get_sheet_by_name("New Title") >>> ws is ws3 is ws4 True
查看workbook中的所有worksheets名称:openpyxl.workbook.Workbook.get_sheet_names()
>>> print(wb.sheetnames) ['Sheet2', 'New Title', 'Sheet1']遍历worksheets:
>>> for sheet in wb: ... print(sheet.title)
单元格可以看作是worksheet的key,通过key去访问单元格中的数据
>>> c = ws['A4']直接返回A4单元格,如果不存在则会自动创建一个。
使用openpyxl.worksheet.Worksheet.cell()方法操作某行某列的某个值:
>>> d = ws.cell(row=4, column=2, value=10)注意:
当worksheet在内存中被创建时,是没有包含cells的,cells是在首次访问时创建.可以循环在内存中创建cells,这时不指定他们的值也会创建该cells些:(创建100x100cells) >>> for i in range(1,101): ... for j in range(1,101): ... ws.cell(row=i, column=j)通过切片Ranges指定许多cells
>>> cell_range = ws['A1':'C2']同样也可以Ranges rows 或者columns :
>>> colC = ws['C'] >>> col_range = ws['C:D'] >>> row10 = ws[10] >>> row_range = ws[5:10]也可以使用 openpyxl.worksheet.Worksheet.iter_rows() 方法:(需要指定行->行,截止列)
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2): ... for cell in row: ... print(cell) <Cell Sheet1.A1> <Cell Sheet1.B1> <Cell Sheet1.C1> <Cell Sheet1.A2> <Cell Sheet1.B2> <Cell Sheet1.C2> 也可以使用 openpyxl.worksheet.Worksheet.iter_cols() 方法:(需要指定列->列,截止行) >>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2): ... for cell in row: ... print(cell) <Cell Sheet1.A1> <Cell Sheet1.B1> <Cell Sheet1.C1> <Cell Sheet1.A2> <Cell Sheet1.B2> <Cell Sheet1.C2>如果你需要遍历所有文件的行或列,可以使用openpyxl.worksheet.Worksheet.rows() 属性:
>>> ws = wb.active >>> ws['C9'] = 'hello world' >>> tuple(ws.rows) ((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>), (<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>), (<Cell Sheet.A3>, <Cell Sheet.B3>, <Cell Sheet.C3>), (<Cell Sheet.A4>, <Cell Sheet.B4>, <Cell Sheet.C4>), (<Cell Sheet.A5>, <Cell Sheet.B5>, <Cell Sheet.C5>), (<Cell Sheet.A6>, <Cell Sheet.B6>, <Cell Sheet.C6>), (<Cell Sheet.A7>, <Cell Sheet.B7>, <Cell Sheet.C7>), (<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>), (<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))或者 openpyxl.worksheet.Worksheet.columns() 属性:
>>> tuple(ws.columns) ((<Cell Sheet.A1>, <Cell Sheet.A2>, <Cell Sheet.A3>, <Cell Sheet.A4>, <Cell Sheet.A5>, <Cell Sheet.A6>, ... <Cell Sheet.B7>, <Cell Sheet.B8>, <Cell Sheet.B9>), (<Cell Sheet.C1>, <Cell Sheet.C2>, <Cell Sheet.C3>, <Cell Sheet.C4>, <Cell Sheet.C5>, <Cell Sheet.C6>, <Cell Sheet.C7>, <Cell Sheet.C8>, <Cell Sheet.C9>))最简单最安全的方法保存workbook是使用openpyxl.workbook.Workbook对象的 openpyxl.workbook.Workbook.save()方法:
>>> wb = Workbook() >>> wb.save('balances.xlsx')保存的默认位置在python的根目录下。注意:会自动覆盖已经存在文件名的文件。
像写一样我们可以导入openpyxl.load_workbook()已经存在的workbook:
>>> from openpyxl import load_workbook >>> wb2 = load_workbook('test.xlsx') >>> print wb2.get_sheet_names() ['Sheet2', 'New Title', 'Sheet1']
读取结果:
Worksheet range(s): [] Worksheet name(s): [u'\u6d3b\u52a8\u8868', u'\u7528\u6237\u4fe1\u606f', u'Sheet3'] Work Sheet Titile: 活动表 Work Sheet Rows: 3 Work Sheet Cols: 5 Total:3 {"1": ["张三", 18, "男", "广州"], "2": ["李四", 20, "女", "湖北"], "3": ["王五", 25, "女", "北京"]}
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
转载于:https://www.cnblogs.com/guanfuchang/p/5970435.html