未提供项目说明

pyxlsx的Python项目详细描述


简介

一个基于openpyxl的像dictionary一样读/写xlsx工作表的包。在

安装

  • 来自pip
pipinstallpyxlsx
  • 下载包并运行设置.py在
^{pr2}$

用法

  • 创建一个新的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
ABCDEF
1str('Unknown')float(30)int(0)
2idproductNameproductTypepriceweightorigin
3001porkmeat2.51000
4002beefmeat4.51000Australia
  • 按行读写工作表 注意:如果有重复的头名称,则只使用第一个。在
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'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我可以使用Hibernate连接不同的数据库并从表中导入数据吗?没有预定义的对象类   java为什么getBoundsInLocal获取错误的坐标?   java在处理并发哈希映射时必须使用锁   java将过滤的JTable数据导出到文本文件   从命令行编译java不使用库   Java使用方法和构造函数绘制图形   java理解优先级队列中的排序方法   尝试解密文件时,java CipherInputStream为空   在Java中将字符串解析为长字符串   java我想在raspberry pi重新启动时在crontabe中执行arecord命令   弹性搜索弹性搜索Lucene公式的Java计算   java使用apachepoi和docx4j读取doc文件   swing显示JavaGUI是否需要特殊处理?   java使用字符串进行数学运算   java在SpringMVC中用JSP初始化菜单项选择表单数据的最佳方法   无多线程的java多客户端程序   swing如何在Java中对BuffereImage进行去饱和?   java在安卓中创建自定义地图