python3如何将docx转换成pdf文件

yipeiwu_com7年前Python基础

本文实例为大家分享了python3将docx转换成pdf文件的具体代码,供大家参考,具体内容如下

直接上代码

# -*- encoding:utf-8 -*-
"""
  author:lgh
"""

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 = r'xxx\xxx.docx'
  output = r'xxx\xxx.pdf'
  # GenerateSupport()
  rc = doc2pdf(input, output)
  if rc:
    print('转换成功')
  else:
    print('转换失败')

if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用selenium实现批量文件下载

python使用selenium实现批量文件下载

背景 实现需求:批量下载联想某型号的全部驱动程序。 一般在做网络爬虫的时候,都是保存网页信息为主,或者下载单个文件。当涉及到多文件批量下载的时候,由于下载所需时间不定,下载的文件名不定,...

Python-Seaborn热图绘制的实现方法

Python-Seaborn热图绘制的实现方法

制图环境: pycharm python-3.6 Seaborn-0.8 热图 import numpy as np import seaborn as sns import...

python实现矩阵打印

python实现矩阵打印

本文实例为大家分享了python实现矩阵打印的具体代码,供大家参考,具体内容如下 之前面试嵌入式软件的一道题,用c实现矩阵打印,考场上并没有写出来,之后总感觉自己写不出来也就没有去实现,...

mac PyCharm添加Python解释器及添加package路径的方法

mac PyCharm添加Python解释器及添加package路径的方法

一、背景 PyCharm执行Python时,找不到自己安装的package,例如pandas、numpy、scipy、scikit等,在执行时报如下错误ImportError: No...

微信小程序python用户认证的实现

微信小程序python用户认证的实现

这里用到django-rest-framework-jwt这个库   https://github.com/GetBlimp/django-rest-framewor...