赞
踩
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 可以遍历整个工作簿: >>>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。