Python 将宏添加到现有的 XLSM
Python Add Macro to an existing XLSM
嗯。我有一个完整的宏 xlsm 数据。我必须在工作簿中添加 Workbook_Open vba 脚本。
但是使用较低的代码,只需将代码添加到模块部分即可。我必须将 vba 代码添加到 'this workbook' 部分。
嗯。将 vbaProject.bin 添加到 xlsm 数据可能更好。但我认为这对于 xlswriter 库是可能的。而且这个库不能添加 vba 到现有的 xlsm :(
谁能帮帮我?
# set file paths and macro name accordingly - here we assume that the files are located in the same folder as the Python script
pathToExcelFile = 'Dir/test.xlsx'
pathToMacro = 'Dir/test/macro.txt'
myMacroName = 'UsefulMacro'
# read the textfile holding the excel macro into a string
with open (pathToMacro, "r") as myfile:
print('reading macro into string from: ' + str(myfile))
macro=myfile.read()
print(macro)
# open up an instance of Excel with the win32com driver
excel = win32com.client.Dispatch("Excel.Application")
# do the operation in background without actually opening Excel
excel.Visible = False
# open the excel workbook from the specified file
workbook = excel.Workbooks.Open(Filename=pathToExcelFile)
# insert the macro-string into the excel file
excelModule = workbook.VBProject.VBComponents.Add(1)
excelModule.CodeModule.AddFromString(macro)
# run the macro
#excel.Application.Run(myMacroName)
xlOpenXMLWorkbookMacroEnabled = 52
# save the workbook and close
#excel.Workbooks(1).Close(SaveChanges=1)
excel.Workbooks(1).SaveAs('testneu.xlsm', FileFormat=xlOpenXMLWorkbookMacroEnabled)
#wb.SaveAs(filename, FileFormat=xlOpenXMLWorkbookMacroEnabled)
excel.Application.Quit()
# garbage collection
del excel
试试这个代码:
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
code = 'Private Sub Workbook_Open() \n \
MsgBox "Hello!" \n \
End Sub'
path = r'c:\Users\Alex20\Documents\test.xlsm'
wb = xl.Workbooks.Open(path)
wb.VBProject.VBComponents(1).CodeModule.AddFromString(code)
#xl.Visible = True
wb.Close(True)
xl.Quit()
Before:
After:
Running:
嗯。我有一个完整的宏 xlsm 数据。我必须在工作簿中添加 Workbook_Open vba 脚本。 但是使用较低的代码,只需将代码添加到模块部分即可。我必须将 vba 代码添加到 'this workbook' 部分。
嗯。将 vbaProject.bin 添加到 xlsm 数据可能更好。但我认为这对于 xlswriter 库是可能的。而且这个库不能添加 vba 到现有的 xlsm :(
谁能帮帮我?
# set file paths and macro name accordingly - here we assume that the files are located in the same folder as the Python script
pathToExcelFile = 'Dir/test.xlsx'
pathToMacro = 'Dir/test/macro.txt'
myMacroName = 'UsefulMacro'
# read the textfile holding the excel macro into a string
with open (pathToMacro, "r") as myfile:
print('reading macro into string from: ' + str(myfile))
macro=myfile.read()
print(macro)
# open up an instance of Excel with the win32com driver
excel = win32com.client.Dispatch("Excel.Application")
# do the operation in background without actually opening Excel
excel.Visible = False
# open the excel workbook from the specified file
workbook = excel.Workbooks.Open(Filename=pathToExcelFile)
# insert the macro-string into the excel file
excelModule = workbook.VBProject.VBComponents.Add(1)
excelModule.CodeModule.AddFromString(macro)
# run the macro
#excel.Application.Run(myMacroName)
xlOpenXMLWorkbookMacroEnabled = 52
# save the workbook and close
#excel.Workbooks(1).Close(SaveChanges=1)
excel.Workbooks(1).SaveAs('testneu.xlsm', FileFormat=xlOpenXMLWorkbookMacroEnabled)
#wb.SaveAs(filename, FileFormat=xlOpenXMLWorkbookMacroEnabled)
excel.Application.Quit()
# garbage collection
del excel
试试这个代码:
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
code = 'Private Sub Workbook_Open() \n \
MsgBox "Hello!" \n \
End Sub'
path = r'c:\Users\Alex20\Documents\test.xlsm'
wb = xl.Workbooks.Open(path)
wb.VBProject.VBComponents(1).CodeModule.AddFromString(code)
#xl.Visible = True
wb.Close(True)
xl.Quit()
Before:
After:
Running: