需要在这个网址,安装一个exe包,地址:https://wkhtmltopdf.org/
进入网址后,点这个位置:
选择一个你的操作系统的下载链接:
安装后的exe文件:
1 |
C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe |
我已经下载好了一个安装包的地址:链接:https://pan.baidu.com/s/1yDP1opcBF3ORUTKBDu8qhQ 提取码:zmo0
1 |
pip install pdfkit |
1 2 3 4 5 6 7 8 9 |
import pdfkit
# wkhtmltopdf.exe 为本地安装的路径 exe_path = r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" config = pdfkit.configuration(wkhtmltopdf=exe_path)
# 调用pdfkit.from_url参数,转换PDF url = "http://antpython.net/webspider/douban_book_list.html" pdfkit.from_url(url, 'Python书籍推荐.pdf', configuration=config) |
其中:C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe是你安装了https://wkhtmltopdf.org/这个网址的exe程序后得到的本地电脑路径;
pdfkit.configuration(wkhtmltopdf=exe_path)这句代码配置了一个pdfkit的配置项
pdfkit.from_url函数有三个参数:1、url,是要读取的网页链接,用于生成PDF的来源URL;2、'Python书籍推荐.pdf',是本地的要存储结果的PDF文件 3、configuration=config,工具配置,主要是上面的指定了的exe的地址