如何迭代工作簿中的工作表,openpyxl [英] How to iterate over worksheets in workbook, openpyxl
问题描述
我一直在使用openpyxl模块对某些.xlsx文件进行一些处理.我一直在尝试找出如何遍历工作簿中的工作表.我不确定是否可以解决这个问题.我试过下面的两个代码都返回空结果.我的.xlsx文件大约有20张,因此应该返回一些内容.
I've been using the openpyxl module to do some processing on some .xlsx files. I've been trying to figure out how to iterate over sheets in a workbook. I'm not sure if I can get it figured out. I've tried the 2 codes below which both return empty results. My .xlsx file has about 20 sheets, so something should return.
我在互联网上找不到的一件事是如何将工作簿设置为实际的工作簿.通常,我正在写工作簿,因此我只是通过将变量设置为空工作簿workbook = Workbook()
来对其进行初始化,但是在这种情况下,我不确定是否可以通过执行workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
The one thing I couldn't find on the internet, is how to set a workbook to an actual workbook. Usually I am writing to a workbook, so I just initialize it by setting a variable to en empty workbook workbook = Workbook()
but in this case, I am unsure if I can open a workbook by doing workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
如果任何人都可以识别出我在做什么错,我将不胜感激.
If anyone can identify what it is I am doing wrong, I would appreciate it.
这是我的代码:
workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in workbook.worksheets:
print sheet
# or
for sheet in workbook.worksheets:
print sheet.title
推荐答案
通过 load_workbook()并遍历worksheets
:
from openpyxl import load_workbook
wb = load_workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in wb.worksheets:
print sheet
这篇关于如何迭代工作簿中的工作表,openpyxl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!