Mirosoft Office自动化问题

我们习惯于将Jenkins slave安装成Windows Service,而微软的Office自动化部分,不支持以Windows Service的方式运行。以下的代码,如果在Jenkins Slave ( As windows service) 中运行,将导致Word打开的时候出错。( doc = word.Documents.Open(in_file) )

import sys
import os
import time
import win32com.client

wdFormatPDF = 17

in_file = r'C:\Users\Administrator\Desktop\Build\ter\forExample.doc'

word = win32com.client.Dispatch('Word.Application')

word.Visible = True
word.ScreenUpdating = False
    
in_file = os.path.join(docDir, doc['docName'])
out_file = in_file.split('.')[0] + '.pdf'

doc = word.Documents.Open(in_file)
time.sleep(15)

doc.Activate()
doc.AcceptAllRevisions()
doc.Revisions.AcceptAll()
if (doc.Comments.Count != 0):
    doc.DeleteAllComments()
    
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()

word.Quit()


解决的办法是:

1. Create a desktop folder for the system profile, as:

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

and, if running on a 64-bit machine, create another one, as:

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

2. The folder(s) need write permission for whatever user is "driving" Office.

Note that, on 64-bit Windows, you need both folders, not just the WOW64 one

 

 

posted @ 2014-01-10 11:51  炉石传说  阅读(453)  评论(0编辑  收藏  举报