暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Python | Word批量转PDF一行代码搞定

跟着小白学Python 2021-06-19
3089

点击上方蓝字关注我们,一起涨姿势!







有时候我们需要将一个Word文档保存为PDF格式进行分享,对于少数的文档直接另存为就可以了,但是大量的转换人工操作就很耗时了。今天给大家介绍一个Python第三方库实现这样的操作,极其简单核心代码只有一行
废话不多说,先安装docx2pdf库
pip install docx2pdf

安装成功


输入docx2pdf -h 查看帮助文件,可以看到其用法很简洁

usage: docx2pdf [-h] [--keep-active] [--version] input [output]

Example Usage:
Convert single docx file in-place from myfile.docx to myfile.pdf:
    docx2pdf myfile.docx
Batch convert docx folder in-place. Output PDFs will go in the same folder:
    docx2pdf myfolder/
Convert single docx file with explicit output filepath:
    docx2pdf input.docx output.docx
Convert single docx file and output to a different explicit folder:
    docx2pdf input.docx output_dir/
Batch convert docx folder. Output PDFs will go to a different explicit folder:
    docx2pdf input_dir/ output_dir/
positional arguments:
  input          input file or folder. batch converts entire folder or convert
                 single file
  output output file or folder

optional arguments:
  -h, --help     show this help message and exit
  --keep-active prevent closing word after conversion
  --version      display version and exit


来试试看:
第一种:单文件转换

from docx2pdf import convert
convert("demo1.docx")

完成

看看效果还是不错的


第二种:指定输出文件名

from docx2pdf import convert
convert("demo2.docx","demo2转换.pdf")

默认会输出在文档目录里


第三种:批量转换文件夹中文档

from docx2pdf import convert
convert("demo")


可以看到文件夹中的文件都进行了转换


也可以指定到其他文件夹

from docx2pdf import convert
convert("demo","demox")

效果如下


怎么样,是不是很简单?

官方文档 https://pypi.org/project/docx2pdf/


扫码二维码获取更多精彩



点个在看你最好看


文章转载自跟着小白学Python,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论