python的代码可以工作,只是一旦它运行了,我仍然会从excel中得到一个框,询问“想要保存您对工作簿的更改”,而我的代码中似乎缺少的是添加一些将在最后保存工作簿的内容。我在工作簿中使用RTD函数,这可能是弹出窗口的原因。
这是我使用的python代码。
from __future__ import print_function
import unittest
import os.path
import win32com.client
class ExcelMacro(unittest.TestCase):
def test_excel_macro(self):
try:
xlApp = win32com.client.DispatchEx('Excel.Application')
xlsPath = os.path.expanduser('C:\Magic Samsung\Watch Samsung\Workbook.xlsm')
wb = xlApp.Workbooks.Open(Filename=xlsPath)
xlApp.Run('ArchiveMaster')
wb.Save()
xlApp.Quit()
print("Macro ran successfully!")
except:
print("Error found while running the excel macro!")
xlApp.Quit()
if __name__ == "__main__":
unittest.main()
发布于 2018-05-31 19:28:04
根据Link的说法,xlOpenXMLWorkbookMacroEnabled
是52岁。因此,在保存时,只给出参数FileFormat = 52,如下面的代码所示
class ExcelMacro(unittest.TestCase):
def test_excel_macro(self):
try:
xlApp = win32com.client.DispatchEx('Excel.Application')
xlapp.DisplayAlerts=False
xlsPath = os.path.expanduser('C:\Magic Samsung\Watch Samsung\Workbook.xlsm')
wb = xlApp.Workbooks.Open(Filename=xlsPath)
xlApp.Run('ArchiveMaster')
wb.SaveAs(Filename=path_here, FileFormat=52)
xlApp.Quit()
print("Macro ran successfully!")
except:
print("Error found while running the excel macro!")
xlApp.Quit()
if __name__ == "__main__":
unittest.main()
注意:使用另存为时会出现一个弹出窗口。这个SO question和This So Post的答案就是这个问题
https://stackoverflow.com/questions/50622888
复制相似问题