|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 zltzlt 于 2020-3-11 12:12 编辑
- from win32com.client import Dispatch, constants, gencache
- import pythoncom
- def word2pdf(word_name, pdf_file_name):
- """
- 将 Word 文档 .docx 转化为 .pdf 文档
- 参数
- ----------
- word_name: str
- Word 文档的名字
- pdf_file_name: str
- 转化后的 .pdf 文档路径(包括文件名)
- 返回值
- -------
- bool
- 如果转化成功则返回 True,否则返回 False
- """
- try:
- pythoncom.CoInitialize()
- gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
- w = Dispatch("Word.Application")
- doc = w.Documents.Open(word_name, ReadOnly=1)
- doc.ExportAsFixedFormat(pdf_file_name, constants.wdExportFormatPDF,
- Item=constants.wdExportDocumentWithMarkup,
- CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
- w.Quit(constants.wdDoNotSaveChanges)
- return True
- except Exception:
- return False
复制代码 |
|