openpyxl模块 官方手册【转】

本文详细介绍了Python的openpyxl模块,包括如何读取、写入和操作Excel文件,帮助开发者高效地处理Excel数据。
摘要由CSDN通过智能技术生成

openpyxl官方手册
教程 Tutorial
创建excel文件 Create a workbook
There is no need to create a file on the filesystem to get started with openpyxl. Just import the Workbook class and start work:
开始使用openpyxl时,无需在文件系统中创建文件,只要导入workbook类就可以了:

>>> from openpyxl import Workbook
>>> wb = Workbook()
A workbook is always created with at least one worksheet. You can get it by using the Workbook.active property:
至少有一个工作表在工作簿创建后,可以通过Workbook.active属性来定位到工作表:

>>> ws = wb.active
Note
This is set to 0 by default. Unless you modify its value, you will always get the first worksheet by using this method.
该工作簿的默认索引是从0开始。除非索引值被修改,否则使用这个方法将总是获取第一个工作表。

You can create new worksheets using the Workbook.create_sheet() method:
可以使用Workbook.create_sheet()方法来创建新工作表

>>> ws1 = wb.create_sheet("Mysheet") # 插入到最后 (默认)

>>> ws2 = wb.create_sheet("Mysheet", 0) # 插入到最前  

>>> ws3 = wb.create_sheet("Mysheet", -1) # 插入到倒数第二
Sheets are given a name automatically when they are created. They are numbered in sequence (Sheet, Sheet1, Sheet2, …). You can change this name at any time with the Worksheet.title property:
工作表将在创建时按照数字序列自动命名(如Sheet,Sheet1,Sheet2,……)。可以在任何时候通过Worksheet.title属性修改工作表名:

>>>ws.title = "New Title"
The background color of the tab holding this title is white by default. You can change this providing an RRGGBB color code to the Worksheet.sheet_properties.tabColor attribute:
创建的工作表的标签背景色默认是白色。可以通过在Worksheet.sheet_properties.tabColor对象中设置RRGGBB格式的颜色代码进行修改:

>>>ws.sheet_properties.tabColor = "1072BA"
Once you gave a worksheet a name, you can get it as a key of the workbook:
当设置了worksheet名称,可以将名称作为工作表的索引:

>>> ws3 = wb["New Title"]
You can review the names of all worksheets of the workbook with the Workbook.sheetname attribute
可以通过Workbook.sheetname对象来查看工作簿中所有工作表的名称

>>> print(wb.sheetnames)
['Sheet2', 'New Title', 'Sheet1']
You can loop through worksheets
可以遍历整个工作簿:

>>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值