Mirosoft Office自动化问题

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

Mirosoft Office自动化问题
import sys
import os
import time
import win32com.client

wdFormatPDF = 17

in_file = rC:\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()
Mirosoft Office自动化问题


解决的办法是:

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

Mirosoft Office自动化问题

上一篇:(JAVA/SWT/JFACE)[不规则窗口/自定义窗口]实现方案!


下一篇:三消的边框描边