openpyxl 按名称获取 sheet
openpyxl get sheet by name
我正在将一些数据写入 Excel 文件,但我不知道如何调整代码以便能够控制我正在写入哪个 sheet:
wb = load_workbook(filename)
active_ws = wb.active
而不是 wb.active
,我怎么能说 Sheets('Data')
(这就是 VBA 语法的样子...)?
你应该使用 wb[sheetname]
from openpyxl import load_workbook
wb2 = load_workbook('test.xlsx')
ws4 = wb2["New Title"]
PS:
你应该检查你的 sheet in sheet names wb.sheetnames
print(wb2.sheetnames)
['Sheet2', 'New Title', 'Sheet1']
import openpyxl
n = 0
wb = openpyxl.load_workbook('D:\excel.xlsx')
sheets = wb.sheetnames
ws = wb[sheets[n]]
参考:
How to switch between sheets in excel openpyxl python to make changes
我正在将一些数据写入 Excel 文件,但我不知道如何调整代码以便能够控制我正在写入哪个 sheet:
wb = load_workbook(filename)
active_ws = wb.active
而不是 wb.active
,我怎么能说 Sheets('Data')
(这就是 VBA 语法的样子...)?
你应该使用 wb[sheetname]
from openpyxl import load_workbook
wb2 = load_workbook('test.xlsx')
ws4 = wb2["New Title"]
PS:
你应该检查你的 sheet in sheet names wb.sheetnames
print(wb2.sheetnames)
['Sheet2', 'New Title', 'Sheet1']
import openpyxl
n = 0
wb = openpyxl.load_workbook('D:\excel.xlsx')
sheets = wb.sheetnames
ws = wb[sheets[n]]
参考: How to switch between sheets in excel openpyxl python to make changes