2012-09-24 54 views
2

我有一個在Windows Server 2008上運行的Python腳本。它打開一個電子表格,並向電子表格中添加一個過濾器行,關閉它然後將其壓縮。主電子表格使用xlwt創建。我只使用Pywin32,因爲用戶想要添加一個過濾器行。任務計劃程序不會運行python腳本(pywin32)來打開Excel。如何從Pywin32獲取更多信息

手動執行此操作時(即直接通過Windows資源管理器或命令行)它可以工作。但是,當通過Task Schduler手動觸發腳本或在必要時觸發腳本時,它不起作用。

'run as'用戶是管理員。我已經檢查過,以確保它以最高的權限運行。

我通過將輸出管道輸出到日誌文件來捕獲錯誤消息。回溯顯示:

File "C:\www\..\main\management\commands\excel_writer.py", line 183, in add_filter_control 
    xl.Workbooks.Open(file_path) 
File "<COMObject <unknown>>", line 8, in Open 
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u'Die Open-Methode des Workbooks-Objektes konnte nicht ausgef\xfchrt werden.', u'C:\\Program Files\\Microsoft Office\\OFFICE11\\1031\\xlmain11.chm', 0, -2146827284), None) 

好吧......這是德文,但它基本上說「工作簿打開方法不能執行」。

但是,我無法弄清楚爲什麼。有沒有人有關於如何找出原因的任何提示?

乾杯

ALJ

下面是一些代碼,並設置:

電子表格創建代碼

... 
w = xlwt.Workbook('UTF-8') 
    worksheets = {} 

    data_formatting_dict = construct_data_formatting() 

    for sheetref, sheetname, datasource, dataformat_ref in sheetlist: 
     worksheets[sheetref] = w.add_sheet(sheetname) 
     fetch_row_heights(sheetref, dataformat_ref) 
     xls_set_columnwidth(sheetref, dataformat_ref) 
     xls_set_main_titles(sheetref, sheetname, dataformat_ref) 
     xls_set_titles(sheetref, sheetname, dataformat_ref) 
     xls_insert_filter_row(sheetref, dataformat_ref) 
     xls_freeze_panes(sheetref, dataformat_ref) 
     xls_write_lines(sheetref,dataformat_ref,datasource,pre_title_rows+title_rows+post_title_rows) 

    filename = fileroot + ".xls" 
    archive = fileroot + ".zip" 
    filepath = os.getcwd() + "\\" + filename 
    w.save(filepath) 

    add_filter_control(filepath) << This is where the filter code is called. 

    ziparchive(filename, archive)  
    full_archive_ref = save_to_folder + archive 
    shutil.copy(archive, full_archive_ref) 

    os.remove(filename) 
    os.remove(archive) 
... 

add_filter_control

def add_filter_control(file_path): 
    try: 
     from win32com.client import Dispatch 
    except: 
     return 

    xl = Dispatch("Excel.Application") 
    xl.Workbooks.Open(file_path) 
    for id, sheetname, source, formatref in sheetlist: 
     cellref = format_params[formatref]['filter_range'] 
     if cellref is not None: 
      xl.ActiveWorkbook.Worksheets(sheetname).Range(cellref).AutoFilter(1) 
    xl.ActiveWorkbook.Close(SaveChanges=1) # 1 is True, 0 is False 
    xl.quit() 

任務調度程序設置

<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
    <RegistrationInfo> 
    <Date>2010-07-23T21:25:09.4036437</Date> 
    <Author>WIN-1CW6Q4GAAAM\Administrator</Author> 
    <Description>Refreshes the temp tables in the database and generates the summary report spreadsheet.</Description> 
    </RegistrationInfo> 
    <Triggers> 
    <CalendarTrigger> 
     <StartBoundary>2010-07-23T05:00:00</StartBoundary> 
     <ExecutionTimeLimit>PT1H</ExecutionTimeLimit> 
     <Enabled>true</Enabled> 
     <ScheduleByDay> 
     <DaysInterval>1</DaysInterval> 
     </ScheduleByDay> 
    </CalendarTrigger> 
    </Triggers> 
    <Principals> 
    <Principal id="Author"> 
     <UserId>WIN-1CW6Q4GAAAM\Administrator</UserId> 
     <LogonType>Password</LogonType> 
     <RunLevel>HighestAvailable</RunLevel> 
    </Principal> 
    </Principals> 
    <Settings> 
    <IdleSettings> 
     <Duration>PT10M</Duration> 
     <WaitTimeout>PT1H</WaitTimeout> 
     <StopOnIdleEnd>true</StopOnIdleEnd> 
     <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings> 
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
    <AllowHardTerminate>true</AllowHardTerminate> 
    <StartWhenAvailable>true</StartWhenAvailable> 
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
    <AllowStartOnDemand>true</AllowStartOnDemand> 
    <Enabled>true</Enabled> 
    <Hidden>false</Hidden> 
    <RunOnlyIfIdle>false</RunOnlyIfIdle> 
    <WakeToRun>false</WakeToRun> 
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit> 
    <Priority>7</Priority> 
    </Settings> 
    <Actions Context="Author"> 
    <Exec> 
     <Command>run_reports.bat</Command> 
     <WorkingDirectory>C:\www\my_site\mysite\</WorkingDirectory> 
    </Exec> 
    </Actions> 
</Task> 
+0

你可以分享計劃任務信息/設置,以及一些/所有的腳本代碼?這將使診斷問題更簡單。 – selllikesybok

+0

好的。請參閱以上修改。 – alj

+0

Office可能不是爲所有用戶安裝的。嘗試使用Windows資源管理器或命令行(即您自己)手動運行任務時,將任務安排爲以同一用戶身份運行。或者,以管理員身份登錄並查看是否可以手動運行它。 – martineau

回答

1

經過搜索,我發現了以下解決方案(完整的線程可以http://social.msdn.microsoft.com/Forums/en/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91被發現)的數天

這樣看來,一個桌面文件夾似乎必須在systemprofile文件夾中通過Excel打開文件。您需要創建這些文件夾中的一個:

的Windows 2008服務器的x64

C:\ WINDOWS \ Syswow64資料\ CONFIG \ systemprofile \桌面

的Windows 2008服務器的x86

Ç :\ Windows \ System32 \ config \ systemprofile \ Desktop

我不確定爲什麼會出現這種情況,但它對我有用。我希望這對於處於同一情況的人有幫助,並感謝那些試圖幫助的人。

相關問題