2

Windows Server 2008 で Python スクリプトを実行しています。スプレッドシートを開き、スプレッドシートにフィルター行を追加し、閉じてから圧縮します。メインのスプレッドシートは xlwt を使用して作成されます。ユーザーがフィルター行を追加したいので、私は Pywin32 のみを使用します。

これを手動で (つまり、Windows エクスプローラーまたはコマンド ラインから直接) 実行すると、機能します。ただし、Task Schduler を介してスクリプトを手動で起動したり、必要な時間にトリガーしたりすると、機能しません。

「実行」ユーザーは管理者です。最高の権限で実行されることを確認しました。

出力をログ ファイルにパイプすることで、エラー メッセージをキャプチャしました。トレースバックは次を示します。

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

1 に答える 1

5

数日間検索した後、次の解決策を見つけました (完全なスレッドはhttp://social.msdn.microsoft.com/Forums/en/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91にあります) 。

Excel でファイルを開くには、systemprofile フォルダーに Desktop フォルダーが必要なようです。次のいずれかのフォルダーを作成する必要があります。

Windows 2008 サーバー x64

C:\Windows\SysWOW64\config\systemprofile\Desktop

Windows 2008 サーバー x86

C:\Windows\System32\config\systemprofile\Desktop

なぜそうなのかはわかりませんが、私にとってはうまくいきました。これが同じ状況の誰かに役立つことを願っています。助けようとした人たちに感謝します.

于 2012-09-28T09:51:47.317 に答える