python3如何将docx转换成pdf文件

yipeiwu_com5年前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实现归一化处理

如何基于python实现归一化处理

这篇文章主要介绍了如何基于python实现归一化处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下    ...

200行自定义python异步非阻塞Web框架

Python的Web框架中Tornado以异步非阻塞而闻名。本篇将使用200行代码完成一个微型异步非阻塞Web框架:Snow。 一、源码 本文基于非阻塞的Socket以及IO多路复用从而...

python web.py开发httpserver解决跨域问题实例解析

使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问题。 跨域请求,就是在浏览器窗口中,和某个服务...

python实现xlsx文件分析详解

python实现xlsx文件分析详解

python脚本实现xlsx文件解析,供大家参考,具体内容如下 环境配置: 1.系统环境:Windows 7 64bit 2.编译环境:Python3.4.3 3.依赖库: os s...

django一对多模型以及如何在前端实现详解

models.py class xm(models.Model): xmID=models.AutoField(primary_key=True) xmTitle=model...