4

I am trying to import data in an .xlsm file as a data frame. When I import the .xlsm file using the command below, it gives me an empty data frames as the result:

exp = pd.read_excel(File_Path+'file_name'.xlsx',sheetname='Data',header=None)

I have manually saved .xlsm files as .xlsx file and imported them as data frame.

Can anybody tell me how to save as .xlsm file as .xlsx using Python?

4

1 回答 1

5

我建议您尝试使用win32com类型方法。如果您在 Windows 上,您可以使用 Python 自动执行 Excel 本身来执行从.xlxm文件到.xlsx格式的转换。然后可以使用通常的 Pandaspd.read_excel()函数正确读取该文件。

这可以按如下方式完成:

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')

# Load the .XLSM file into Excel
wb = excel.Workbooks.Open(r'input.xlsm')

# Save it in .XLSX format to a different filename
excel.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs(r"output.xlsx", FileFormat=51, ConflictResolution=2)
excel.Application.Quit()

# Load the .XLSX file into Pandas
df = pd.read_excel('output.xlsx', sheetname='Data', header=None)

我建议您添加文件名的完整路径。

于 2016-09-05T16:10:20.020 回答