我有以下代码:
import pandas as pd
import win32com.client
excel_app = win32com.client.Dispatch("Excel.Application")
file_path = r"path to the file"
file_password = "file password"
workbook = excel_app.Workbooks.Open(file_path, Password=file_password)
sheet = workbook.Sheets("sheet name")
现在我想获取工作表变量并将其加载到 Pandas df 中。我试图通过将工作表保存到一个单独的文件然后从 Pandas 读取它来完成它,但这似乎使问题过于复杂,因为该文件既受密码保护又采用 .xlsm 格式,因此重新打开它直接来自 Pandas 并不简单。
我该怎么做?
最佳答案
工作表的 UsedRange 属性将返回一个包含工作表中所有包含数据的单元格的数组。
df = pd.DataFrame(sheet.UsedRange())
以列标题为列号,索引为行号。都是从零开始。
关于python - 如何将 win32com Excel 工作表加载到 Pandas df?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67718145/