# 默认会新建一个 Sheet 页 wb = Workbook() ws1 = wb.create_sheet("Mysheet2") # insert at the end (default) 最后位置 # or ws2 = wb.create_sheet("Mysheet3", 0) # insert at first position 首个位置 # or ws3 = wb.create_sheet("Mysheet4", -1) # insert at the penultimate position 倒数第二
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>
同样,Worksheet.iter_cols() 方法将返回列:
1 2 3 4 5 6 7 8 9
for col in ws.iter_cols(min_row=1, max_col=3, max_row=2): for cell in col: print(cell) <Cell Sheet1.A1> <Cell Sheet1.A2> <Cell Sheet1.B1> <Cell Sheet1.B2> <Cell Sheet1.C1> <Cell Sheet1.C2>
from tempfile import NamedTemporaryFile from openpyxl import Workbook wb = Workbook() with NamedTemporaryFile() as tmp: wb.save(tmp.name) tmp.seek(0) stream = tmp.read()
Loading from a file
You can use the openpyxl.load_workbook() to open an existing workbook:
1 2 3
from openpyxl import load_workbook
wb = load_workbook(filename = 'empty_book.xlsx')
参考
openpyxl - A Python library to read/write Excel 2010 xlsx/xlsm files — openpyxl documentation
Series 是 Pandas 中的一个核心数据结构,类似于一个一维的数组,具有数据和索引。
Series 可以存储任何数据类型(整数、浮点数、字符串等),并通过标签(索引)来访问元素。
Series 的数据结构是非常有用的,因为它可以处理各种数据类型,同时保持了高效的数据操作能力,比如可以通过标签来快速访问和操作数据。