1. 安装依赖的exe
需要在这个网址,安装一个exe包,地址:https://wkhtmltopdf.org/
进入网址后,点这个位置:
选择一个你的操作系统的下载链接:
安装后的exe文件:
c:\program files\wkhtmltopdf\bin\wkhtmltopdf.exe
我已经下载好了一个安装包的地址:链接:https://pan.baidu.com/s/1ydp1opcbf3orutkbdu8qhq 提取码:zmo0
2. 安装pip库
pip install pdfkit
3. 执行转换
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的地址
4. 转换后的效果
到此这篇关于python读取url生成pdf的方法步骤的文章就介绍到这了,更多相关python读取url生成pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论