转 pdf
转 pdf 中运用 pdfkit 库,它能够让 web 网页直接转为 pdf 文件,多个 url 能够合并成一个文件。
装置 pdfkit 库
装置 wkhtmltopdf 文件
pdfkit 是基于 wkhtmltopdf 的 python 封装库,所以需求装置 wkhtmltopdf 软件。
下载地址:https://wkhtmltopdf.org/downloads.html
在windows 体系中,需求将 wkhtmltopdf.exe 文件途径装备在体系环境变量中。
url 生成 pdf
这里运用 baidu 主页和 bing 主页作为示例
import pdfkit # 第一个参数能够是列表,放入多个域名,第二个参数是生成的 PDF 称号 pdfkit.from_url(['www.baidu.com','www.bing.com'],'search.pdf')
本地 html 文件生成 pdf
提前将需求转化的 html 存储到本地,也能够运用 python 爬虫代码抓取 html 文件到本地。
import pdfkit pdfkit.from_file('/Users/xx/Desktop/html/baidu.html', 'search.pdf')
转 word
运用 pypandoc 库将 html 转化为 word 文件,pypandoc 是一个支撑多种文件格式转化的 Python 库,它用到了 pandoc 软件,所以需求在电脑上装置 pandoc 软件
装置 pypandoc 库
装置 pandoc 软件
pypandoc 是基于 pandoc 软件的库,所以要装置一下 pandoc (https://github.com/jgm/pandoc/releases/tag/2.11.4),pandoc 支撑多种类型转化。下图是 pandoc 的转化类型。
运用
将 html 文件提前存储在本地,也能够用爬虫将需求转化的 html 文件在代码中抓取后运用。
import pypandoc # convert_file('原文件','方针格式','方针文件') output = pypandoc.convert_file('/Users/xx/Desktop/html/baidu.html', 'docx', outputfile="baidu.doc")
pypandoc 无法对 word 进行排版,所以需求小伙伴们进行 2 次排版。
总结
利用好 Python 第三方库类,能够为小伙伴写出各种个性化定制的小程序