手机python编程文件如何转文档_python3如何将docx转换成pdf文件

本文实例为大家分享了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}',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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值