一、在windows系统中
"""
windows系统下实现docx->pdf
"""
from win32com.client import Dispatch, constants, gencache
def doc2pdf(input, output):
w = Dispatch('Word.Application')
try:
doc = w.Documents.Open(input, ReadOnly=1)
doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
return True
except:
return False
finally:
w.Quit(constants.wdDoNotSaveChanges)
def GenerateSupport():
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
def main():
input_file = r'绝对地址/文件名.docx'
output_file = r'绝对地址/文件名.pdf'
rc = doc2pdf(input_file , output_file )
if rc:
print('转换成功')
else:
print('转换失败')
if __name__ == '__main__':
main()
2.在linux系统中
"""
linux platform word to pdf
"""
import subprocess
import os
try:
from comtypes import client
except ImportError:
client = None
try:
from win32com.client import constants, gencache
except ImportError:
constants = None
gencache = None
def doc2pdf_linux(docPath, pdfPath):
"""
允许的文档格式:doc,docx
仅在linux平台下可以
需要在linux中下载好libreoffice
"""
cmd = 'libreoffice --headless --convert-to pdf'.split() + [docPath] + ['--outdir'] + [pdfPath]
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait(timeout=30)
stdout, stderr = p.communicate()
if stderr:
raise subprocess.SubprocessError(stderr)
def doc2pdf(docPath, pdfPath):
"""
注意使用绝对路径
pdf的生成只写路径,不写名字
"""
docPathTrue = os.path.abspath(docPath)
if client is None:
return doc2pdf_linux(docPathTrue, pdfPath)
word = gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(docPathTrue, ReadOnly=1)
doc.ExportAsFixedFormat(pdfPath,
constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
word.Quit(constants.wdDoNotSaveChanges)
if __name__ == '__main__':
wordpath='/myproject/jsg/media/attached/测试文档.docx'
pdfpath='/myproject/jsg/media/attached'
doc2pdf(wordpath,pdfpath)