如何使用Python将.xlsm(启用了宏的Excel文件)转换为.xlsx? [英] How to convert .xlsm (macro enabled excel file) to .xlsx using Python?

查看:1900
本文介绍了如何使用Python将.xlsm(启用了宏的Excel文件)转换为.xlsx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将.xlsm文件中的数据作为数据框导入.当我使用下面的命令导入.xlsm文件时,结果为我提供了一个空的数据帧:

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)

我已经将.xlsm文件手动保存为.xlsx文件,并将它们作为数据框导入.

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

有人可以告诉我如何使用Python将.xlsm文件另存为.xlsx吗?

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

推荐答案

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

I would suggest you try using a win32com type approach. If you are on Windows, you can use Python to automate Excel itself to carry out the conversion from an .xlxm file to .xlsx format. The file can then be read correctly using the usual Pandas pd.read_excel() function.

这可以如下进行:

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)

我建议您为文件名添加完整路径.

I suggest you add full paths to the filenames.

这篇关于如何使用Python将.xlsm(启用了宏的Excel文件)转换为.xlsx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆