未提供项目说明
pyxlsx的Python项目详细描述
简介
一个基于openpyxl的像dictionary一样读/写xlsx工作表的包。在
安装
- 来自pip
pipinstallpyxlsx
- 下载包并运行设置.py在
用法
- 创建一个新的xlsx文件并写入该文件
frompyxlsximportnew_xlsxwithnew_xlsx(filename)aswb:ws=wb.create_sheet('sheet1')# some operations# orwb=new_xlsx()ws=wb.creat_sheet('sheet1')# create a new sheet with name 'sheet1'# some operationswb.save(filename)
- 打开现有的xlsx文件
frompyxlsximportopen_xlsxwithopen_xlsx(filename)aswb:ws1=wb.active# get active sheetws2=wb['sheet2']# some operations# orwb=open_xlsx(filename)ws=wb['sheet2']# some operationswb.save()# to save as another filewb.save(another_filename)
- 向工作表追加行
ws=wb['sheet1']ws.append(["","","str('Unknown')","float(4.5)","int(500)"])# keys can only be of type strcontent1={'id':'001','productName':'pork','productType':'meat','price':2.5,'weight':1000,}content2={'id':'002','productName':'beef','productType':'meat','price':4.5,'weight':1000,'origin':'Australia'}# header is auto-generated from keys of the dict the first time append_by_header is called.ws.append_by_header(content1)# new header name will be append to header if append_header is True (default value)ws.append_by_header(content2)# below is the result of writing operation
A | B | C | D | E | F | |
---|---|---|---|---|---|---|
1 | str('Unknown') | float(30) | int(0) | |||
2 | id | productName | productType | price | weight | origin |
3 | 001 | pork | meat | 2.5 | 1000 | |
4 | 002 | beef | meat | 4.5 | 1000 | Australia |
- 按行读写工作表 注意:如果有重复的头名称,则只使用第一个。在
ws=wb['sheet1']assertws.headerisNonews.header_row=2# set the second row as worksheet header rowassertws.headerisnotNoneforrowinws.content_rows:# starting from row just below header rowprint(row[1])# row cell value can accessed by column number, if key is of type intprint(row['productName'])# row cell value can be accessed by header name, if key is of type of strprint(row['price'])ifrow['productName']=='pork':row[1]='003'# change pork id to '003'row['price']=3.5# change pork price to 3.5# output as below# '001'# 'pork'# 2.5# '002'# 'beef'# 4.5
- 按列阅读工作表
ws=wb['sheet1']ws.header_row=2# get a full columncolumn_cells=ws['B']forcincolumn_cells:print(c.data)# 'pork', 'beef'# get a content column (containing only cells below header) by header name, # if key is of type strname_column=ws.get_content_column('productName')forcinname_column:print(c.data)# 'pork', 'beef'# get a content column by column number,# if key is of type intname_column=ws.get_content_column(2)forcinname_column:print(c.data)# 'pork', 'beef'
- 直接从工作表、页眉、ContentRow读取单元格
ws=wb['sheet1']ws.header_row=2# access a cell by coordinate (row, column)cell=ws.cell(2,2)print(cell.data)# 'productName'# access a cell by header name if key is of type strcell=ws.header.cell('productName')print(cell.data)# 'productName'# access a cell by column numbercell=ws.header.cell(1)print(cell.data)# 'id'forrowinws.content_rows:cell=row.cell(1)# '001', '002'print(cell.data)cell=row.cell('productName')print(cell.data)# 'pork', 'beef'
- 读取某个单元格的相邻单元格
cell=ws.cell(2,2)print(cell.top.data)# "str('Unknown')"print(cell.left.data)# 'id'print(cell.right.data)# 'productType'print(cell.bottom.data)# 'pork'forcincell.vertical:print(c.data)# 'productName', 'pork', 'beef'forcincell.horizontal:print(c.data)# 'productName', 'productType', 'price', 'weigth', 'origin'
- 项目
标签: