我们习惯于将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