2012-11-27 62 views
3

我寫這封信是因爲後看似搜索一半的互聯網劇本,我無法弄清楚爲什麼這個簡單的代碼是不工作:的Python:用於運行Excel VBA腳本不運行

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') 

它應該運行Excel文件「TestDatei.xls」中包含的vba腳本「Makro_test」。我在辦公室的64位Windows 7桌面機上嘗試了Python和Java版本(32和64位)的不同組合。我還在路徑中嘗試了不同的反斜槓和反斜槓組合(反斜槓,簡單,雙)。不幸的是,錯誤信息是德文的。但如果你們中的一些可以檢測到任何東西了這一點,那就是:

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) 
+4

你試過逃避反斜槓嗎? – filmor

+0

我只是想建議用'r'C:\ AC_Software \ TestDatei.xls''或'C:\\ AC_Software \\ TestDatei.xls''替換'C:\ AC_Software \ TestDatei.xls'' 。但是因爲'A'和'T'都是大寫字母,所以它必須是別的... – glglgl

+1

@FrédéricHamidi你試過了嗎?在這裏,''\ a \ A \ t \ T''解析爲''\ x07 \\ A \ t \\ T'',所以'\ A \ T'解析爲'\\ A \\ T'。 – glglgl

回答

0

這裏的英文錯誤消息:

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)

確保你的道路是正確的,並嘗試原始字符串r'C:\AC_Software\TestDatei.xls'代替。我嘗試了相同的代碼,並使用了我的文件。

+0

非常感謝您的回答Mark很遺憾,它並沒有爲我工作:(也許你可以閱讀我的最後一條評論,看看有關Python,IDE,我使用的操作系統和JRE對你來說很有意義。 –

0

請參閱下面的代碼使用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()