avatar

python脚本 Doc 转 PDF

有时候,python脚本就是香啊,省了下载安装软件的麻烦事,提高了工作效率。

########################
# word 转 pdf
########################

import os
from win32com import client

from win32com.client import gencache
from win32com.client import constants, gencache

def createPdf(wordPath, pdfPath):
"""
word转pdf
:param wordPath: word文件路径
:param pdfPath: 生成pdf文件路径
"""
word = gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(wordPath, ReadOnly=1)
doc.ExportAsFixedFormat(pdfPath,
constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
word.Quit(constants.wdDoNotSaveChanges)
if __name__ == "__main__":
doc_name = "C:\\Users\\14768\\Desktop\\1.docx"
fpt_name = "C:\\Users\\14768\\Desktop\\1.pdf"
createPdf(doc_name,fpt_name)
Author: Hui Ning
Link: https://angelni.github.io/doc-to-pdf/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付宝
    支付宝

Comment