亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 win32com python 庫將 VBA 宏應用于 Excel 文件

使用 win32com python 庫將 VBA 宏應用于 Excel 文件

收到一只叮咚 2022-06-02 10:40:58
我嘗試使用 python 和 win32com 庫應用 VBA 宏,想法是創建一個固定的 excel 文件,存儲一個宏然后運行它。我從這里得到了這個想法:https ://redoakstrategic.com/pythonexcelmacro/當腳本必須使用 xlsm 文件并使用 Application.Run() 運行宏時,就會出現問題。我的代碼是:import pandas as pd import win32com.client import os#creating the dataframedf = 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 xlsmshutil.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 workbookfilename_macro = r"C:\Users\John\Desktop\Python_scripts\Running VBA Macro\macros_excel_file.xlsm"workbook = writer.bookworkbook.filename = filename_macroworkbook.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)我轉到 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 )。但是錯誤繼續發生如果有人知道如何解決它會很棒
查看完整描述

1 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

因為.xlsxto.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 編寫器對象來遷移宏并另存為.xlsm. 此外,在編寫器對象上使用上下文with管理器在處理后有效地關閉 i/o 對象。


# 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()


查看完整回答
反對 回復 2022-06-02
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號