word转pdf

很多人在工作经常会遇到word转pdf功能,word转pdf还是比较复杂,网上各种包,如python的各种转换包,其实是存在很多问题

的,尤其是对比较复杂的格式,真正的还的是调用组件来转换,这里介绍的是LibreOffice,以MAC环境为例,没有装LibreOffice的

可以通过brew install LibreOffice 安装,安装成功具体应用代码如下:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:Eric.yue
 4 
 5 from subprocess import Popen, PIPE
 6 def convert(src, dst):
 7     d = {'src': src, 'dst': dst}
 8 
 9     command = '/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --invisible --convert-to pdf %(src)s --outdir %(dst)s' % d
10 
11     process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # I am aware of consequences of using `shell=True`
12     out, err = process.communicate()
13     errcode = process.returncode
14     if errcode != 0:
15         raise Exception(err)
16 
17 if __name__ == '__main__':
18     src = "template.docx"
19     dst = "output_folder"
20     convert(src, dst)

如果是在Linux上应用请直接将/Applications/LibreOffice.app/Contents/MacOS/soffice修改libreoffice

前提是你的事先安装好Libreoffice

转载于:https://www.cnblogs.com/gide/p/10689005.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值