Python-VBA改进

问题描述 投票:0回答:1

我有一个熊猫数据框df。然后,将其另存为excel文件Report.xlsm,然后添加VBA代码以过滤此数据。

代码运行良好。但是,当我执行VBA代码时,Excel总是显示消息框,要求保存Report.xlsm。有没有人知道如何摆脱此消息并自动保存它,而不是必须单击“保存”?

如果不单击保存,Excel文件将被破坏,代码也将崩溃。

Microsoft Excel, ask to save file notification

Python代码:

desktop = os.path.normpath(os.path.expanduser("~/Desktop"))

# Generate excel file
writer = pd.ExcelWriter(desktop + '\Report.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, index = False, sheet_name = 'Report')

workbook = writer.book
workbook.filename = desktop + '\Report.xlsm'

# Add VBA code to new excel report - Report.xlmx  from vbaProject.bin  
workbook.add_vba_project('vbaProject.bin')
writer.save()

# RUN MACRO
exel = win32com.client.Dispatch('Excel.Application')

exel.Workbooks.Open(Filename = desktop + '\Report.xlsm' , ReadOnly=1)
xl.Application.Run("Macro1")          

xl.Application.Quit()
del xl
python vba win32com
1个回答
0
投票

在代码末尾添加此内容

exel.ActiveWorkbook.Close(True)

True =保存,False =不保存

© www.soinside.com 2019 - 2024. All rights reserved.