I am writing because after seemingly searching half the internet, I could not figure out why this simple piece of code is not working:
我写作是因为看似搜索了一半的互联网后,我无法弄清楚为什么这段简单的代码不起作用:
from win32com.client import Dispatch
def RunExcelMacro(name):
myExcel = Dispatch('Excel.Application')
myExcel.Visible = 0
myExcel.Workbooks.Add('C:\AC_Software\TestDatei.xls')
myExcel.Run(name)
myExcel.DisplayAlerts = 0
myExcel.Quit()
if __name__ == "__main__":
RunExcelMacro('Makro_test')
It is supposed to run the vba script "Makro_test" contained in the Excel file "TestDatei.xls". I tried different combinations of Python and Java versions (32 and 64bit) on the 64bit Windows 7 desktop machine at my office. I also tried different slah and backslash combinations in the path (back- and forward slash, simple, double). Unfortunately, the error message is in German. But in case some of you can detect anything out of this, here it is:
它应该运行Excel文件“TestDatei.xls”中包含的vba脚本“Makro_test”。我在办公室的64位Windows 7桌面计算机上尝试了Python和Java版本(32和64位)的不同组合。我还在路径中尝试了不同的slah和反斜杠组合(后退和正斜杠,简单,双精度)。不幸的是,错误消息是德语。但如果你们中的一些人可以发现任何事情,这里是:
Traceback (most recent call last):
File "C:\Users\alloun\workspace\MyTestProject\root\nested\example.py", line 22, in <module>
RunExcelMacro('Makro_test')
File "C:\Users\alloun\workspace\MyTestProject\root\nested\example.py", line 16, in RunExcelMacro
myExcel.Workbooks.Add('C:\AC_Software\TestDatei.xls')
File "<COMObject <unknown>>", line 2, in Add
pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, u'Microsoft Excel', u"'TestDatei.xls' wurde nicht gefunden. \xdcberpr\xfcfen Sie die Rechtschreibung des Dateinamens, und \xfcberpr\xfcfen Sie, ob der Speicherort der Datei korrekt ist.\n\nWenn Sie versuchen, die Datei \xfcber die Liste der zuletzt ge\xf6ffneten Dateien zu \xf6ffnen, stellen Sie sicher, dass die Datei nicht umbenannt, verschoben oder gel\xf6scht wurde.", u'xlmain11.chm', 0, -2146827284), None)
2 个解决方案
#1
0
Here's the English error message:
这是英文错误消息:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"'TestDatei.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", 'xlmain11.chm', 0, -2146827284), None)
pywintypes.com_error:(-2147352567,'异常发生。',(0,u'Microsoft Excel',u“'TestDatei.xls'找不到。检查文件名的拼写,并验证文件位置是正确。\ n \ n如果您尝试从最近使用的文件列表中打开文件,请确保该文件尚未重命名,移动或删除。“,'xlmain11.chm',0,-2146827284) , 没有)
Make sure you the path is correct and try a raw string r'C:\AC_Software\TestDatei.xls'
instead. I tried the same code and it worked with a file I had.
确保路径正确并尝试原始字符串r'C:\ AC_Software \ TestDatei.xls'。我尝试了相同的代码,它使用了我的文件。
#2
0
Please see below the code for running an Excel macro using python. You can find the code in this Site - Link.
请参阅下面使用python运行Excel宏的代码。您可以在本网站 - 链接中找到代码。
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:\test1\test2\test3\test4\MacroFile.xlsm')
wb = xlApp.Workbooks.Open(Filename=xlsPath)
xlApp.Run('macroName')
wb.Save()
xlApp.Quit()
print("Macro ran successfully!")
except:
print("Error found while running the excel macro!")
xlApp.Quit()
if __name__ == "__main__":
unittest.main()
#1
0
Here's the English error message:
这是英文错误消息:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"'TestDatei.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", 'xlmain11.chm', 0, -2146827284), None)
pywintypes.com_error:(-2147352567,'异常发生。',(0,u'Microsoft Excel',u“'TestDatei.xls'找不到。检查文件名的拼写,并验证文件位置是正确。\ n \ n如果您尝试从最近使用的文件列表中打开文件,请确保该文件尚未重命名,移动或删除。“,'xlmain11.chm',0,-2146827284) , 没有)
Make sure you the path is correct and try a raw string r'C:\AC_Software\TestDatei.xls'
instead. I tried the same code and it worked with a file I had.
确保路径正确并尝试原始字符串r'C:\ AC_Software \ TestDatei.xls'。我尝试了相同的代码,它使用了我的文件。
#2
0
Please see below the code for running an Excel macro using python. You can find the code in this Site - Link.
请参阅下面使用python运行Excel宏的代码。您可以在本网站 - 链接中找到代码。
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:\test1\test2\test3\test4\MacroFile.xlsm')
wb = xlApp.Workbooks.Open(Filename=xlsPath)
xlApp.Run('macroName')
wb.Save()
xlApp.Quit()
print("Macro ran successfully!")
except:
print("Error found while running the excel macro!")
xlApp.Quit()
if __name__ == "__main__":
unittest.main()