[英]Appliying a VBA macro to an Excel file using win32com python library
我嘗試使用 python 和 win32com 庫應用 VBA 宏,其想法是創建一個固定的 excel 文件,存儲宏然后運行它。 我從這里得到了這個想法: https://redoakstrategic.com/pythonexcelmacro/
當腳本必須使用 xlsm 文件並使用 Application.Run() 運行宏時,就會出現問題。 我的代碼是:
import pandas as pd
import win32com.client
import os
#creating the dataframe
df = df1=pd.read_excel ("normal_excel_file.xlsx", index_col=0)
filename = "normal_excel_file.xlsx"
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
df.to_excel(writer, sheet_name='data', index=False)
#copiying and renaming the file to xlsm
shutil.copy("normal_excel_file.xlsx", "(1)normal_excel_file.xlsx")
os.rename("(1)normal_excel_file.xlsx", "macros_excel_file.xlsm")
#adding the macro to the workbook
filename_macro = r"C:\Users\John\Desktop\Python_scripts\Running VBA Macro\macros_excel_file.xlsm"
workbook = writer.book
workbook.filename = filename_macro
workbook.add_vba_project('vbaProject.bin')
writer.save()
和沖突的部分:
if os.path.exists(filename_macro):
xl = win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)
#assuming that there is only one macro, and is stored as "ThisWorkbook.Macro1" in the file
xl.Application.Run("ThisWorkbook.Macro1") #I also try using only "Macro1" and the whole path of the file
xl.Application.Quit()
del xl
我得到下一個錯誤。 首先是錯誤信息:
com_error: (-2147352567, 'An exception occurred.', (0, 'Microsoft Excel', 'The "ThisWorkbook.Macro1" macro cannot be executed. The macro may not be available in this book or all of them may have been disabled. the macros. ',' xlmain11.chm ', 0, -2146827284), None)
以及整個錯誤文本:
com_error Traceback (most recent call last)
<ipython-input-16-6e91b8d2f622> in <module>
3 xl = win32com.client.Dispatch('Excel.Application')
4 xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)
----> 5 xl.Application.Run("ThisWorkbook.Macro1")
6 xl.Application.Quit()
7 del xl
~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in Run(self, Macro, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)
~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args)
285
286 def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
--> 287 result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
288 return self._get_good_object_(result, user, resultCLSID)
289
com_error: (-2147352567, 'An exception occurred.', (0, 'Microsoft Excel', 'The "ThisWorkbook.Macro1" macro cannot be executed. The macro may not be available in this book or all of them may have been disabled. the macros. ',' xlmain11.chm ', 0, -2146827284), None)
我 go 到 Microsoft Office 中的信任中心,並允許所有宏類型( https://support.office.com/en-us/article/enable-or-disable-macros-in-office-files-12b036fd-d140 -4e74-b45e-16fed1a7e5c6?ui=en-US&rs=en-US&ad=US )。 但是錯誤繼續發生
如果有人知道如何解決它會很棒
謝謝大家
因為.xlsx
到.xlsm
是根本不同類型的二進制文件,所以不能簡單地使用行進行復制和重命名:
shutil.copy("normal_excel_file.xlsx", "(1)normal_excel_file.xlsx")
os.rename("(1)normal_excel_file.xlsx", "macros_excel_file.xlsm")
(奇怪的是,您的教程鏈接沒有顯示 Pandas 生成的.xlsx
文件如何在后續附加的宏步驟中變為.xlsm
。)
相反,使用 Excel 編寫器 object 遷移宏並另存為.xlsm
。 此外,在編寫器object 上with
上下文管理器以在處理后有效關閉 i/o object。
# creating the dataframe
path = "C:\Users\John\Desktop\Python_scripts\Running VBA Macro"
filename = "normal_excel_file.xlsx"
filename_macro = "macros_excel_file.xlsm"
df = pd.read_excel(filename, index_col=0)
with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='data', index=False)
workbook = writer.book
workbook.filename = filename_macro
workbook.add_vba_project('vbaProject.bin')
writer.save()
# RUN MACRO
if os.path.exists(os.path.join(path, filename_macro)):
wb = xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)
xl.Application.Run("ThisWorkbook.Macro1")
xl.Application.Quit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.