当前位置:网站首页>卧槽,一行代码就可将网页直接转pdf保存下来(pdfkit)
卧槽,一行代码就可将网页直接转pdf保存下来(pdfkit)
2022-06-21 07:28:00 【Python和数据分析】
大家好,我是翔宇!
今天给大家分享一个神器,这个神器可以将网页直接进行保存成pdf下来,可以是网站上的在线网页,可以是本地的html文件,还可以是你想要放进去的文本内容。
这个神器就是pdfkit,而它依赖于wkhtmltopdf,所以在使用pdfkit时必须同时安装wkhtmltopdf,而且为了让在pdfkit在执行的时候能够找到wkhtmltopdf并能顺利调用它,因此还要将wkhtmltopdf的安装路径加入环境变量,便可顺利执行,不然将报错OSError: No wkhtmltopdf executable found。下面将带大家从pdfkit和wkhtmltopd的安装到演示将三种形式的网页内容转换成pdf并保存下来的实例。
1.安装pdfkit和wkhtmltopdf
安装pdfkit比较简单,直接采用我们常用pip进行安装就可以,命令:
pip install pdfkit
jupyter notebook中请用:
!pip install pdfkit
而安装wkhtmltopdf需要先下载离线安装包都本地,进行安装后还要将其安装路径添加到环境变量中,以便pdfkit能够顺利调用。
下载地址:https://wkhtmltopdf.org/downloads.html
然后会根据自己的系统进行下载对应的版本的wkhtmltopdf,直接进行安装,安装过程中留意将安装的路径保存下来,下面要将其添加到环境变量中。

我的电脑是windows系统,所以这里就只演示windows下的安装,安装好wkhtmltopdf之后便是添加环境变量,我们找到安装的路径
将上面的路径复制下来,我的是:C:\Program Files\wkhtmltopdf\bin
1.1添加环境变量
步骤:找到“此电脑” --> 鼠标右键,点击“属性” --> ”高级系统设置“ --> “环境变量” --> “” --> 点击“Path”,”编辑“ --> 编辑环境变量(看下图) --> 确定保存
找到这个界面

编辑环境变量

一路点击确定就行了,如果还是不会请自行百度或者后台添加作者。
2.将三种不同网页内容转换成pdf的演示
上述工作都做好之后,就可以直接进行html转pdf了,在下面演示中,如果你遇到了报错OSError: No wkhtmltopdf executable found,那么你一般式和环境变量没有添加好导致的错误,当然如果是在添加不好环境变量,你也可以加上下面的代码进行运行。
path_wk = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' #你的wkhtmltopdf安装位置
config = pdfkit.configuration(wkhtmltopdf = path_wk)
url = 'https://www.baidu.com/' # 你要转的网页链接
pdfkit.from_url(url, r'D:\data\baidu.pdf', configuration=config) # 你要保存到的路径及pdf名字
(1) 直接将在线网页进行转换pdf:
例如我将下面这个网页的内容转换pdf保存下来。

代码非常简单:
import pdfkit
url = 'https://www.runoob.com/pandas/pandas-tutorial.html'
pdfkit.from_url(url, r'test1.pdf')
然后就会发现你当前运行环境中多了一个叫test1.pdf的pdf文件
打开后是这个样子的,和网页上的主要内容没啥区别。

(2) 将本地html文件转换pdf:
import pdfkit
wkhtmltopdf_options = {
'enable-local-file-access': None
}
pdfkit.from_file('NumPy 安装 _ 菜鸟教程.html', r'test2.pdf')
但是报错了,还没解决,希望知道的小伙伴一起交流:
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError
(3) 将自定义内容转换到pdf:
import pdfkit
pdfkit.from_string('<html><style> @font-face{font-family: "宋体";src:url("C:\\Windows\\Fonts\\simsun.ttf")</style><h1>wkhtmltopdf 你好</h1></html>', r'test2.pdf')
out:True
打开test.pdf后我发现中文有乱码问题(目前还未解决,希望知道的小伙伴可以指导一下)

3.结束语
虽然感觉这个库用起来有些坑,老师报各种各样的问题,但是不可否认的是它真的很强大,对于一些pdf阅读需求直接使用pdfkit就可以很方便的把需要的网页直接拿到本地随时像电子书一样打开看。关于本文涉及的相关安装包或者代码,或者需要交流的地方,欢迎小伙伴随时到公众号***“Python和数据分析”***联系翔宇!下篇内容,我将分享pdfkit与selenuim联合进行实际需求实现的例子,欢迎大家进行阅读。
边栏推荐
- Black technology, real-time voice simulation
- arduino有关软件卸载,库的卸载问题
- RDKit | 基于Murcko骨架聚类化合物库
- Enterprise level development and use POI stepping on pit inventory
- Market trend report, technical innovation and market forecast of scaffold free 3D cell culture plate in China
- Mathematics is a tool for solving problems
- Getting started with MATLAB
- Tensorrt notes (III) reference
- 数学是用于解决问题的工具
- App Safety Penetration Test detailed Method Flow
猜你喜欢

根因解析 | Kubernetes Pod状态异常九大场景盘点

Easyexcel introduction-01

Scaler in scikit learn

Transport layer TCP header - serial number and acknowledgement number
![[transfer] liurun: don't discuss business with people without logic](/img/e7/7881a9f02c7773f92702f6b0232125.png)
[transfer] liurun: don't discuss business with people without logic

Cloud native enthusiast weekly: Chaos mesh upgraded to CNCF incubation project

How to see who developed the applet (see the method of the applet development company)

mysql的安装路径如何查看

IDM mobile terminal function upgrade description

flutter jpush
随机推荐
怎么看小程序是谁开发的(查看小程序开发公司方法)
Integrating eslint in old projects [02]
Course design of supply chain modeling and simulation based on Flexsim
微信小程序_5,全局配置
[DB written interview 390] what is the external table of oracle?
【蓝桥杯单片机组】串口通信
Wechat applet_ 5. Global configuration
RDKit | 基于Murcko骨架聚类化合物库
部署Zabbix企业级分布式监控
Market trend report, technical innovation and market forecast of inorganic microporous adsorbents in China
2022 运维故障案例经验
建设数字化工厂的四个必要步骤
传输层 TCP首部-序号和确认号
Research Report on market supply and demand and strategy of inorganic feed phosphate industry in China
x86 CPU访问DRAM和PCI
基于Flexsim的供应链建模与仿真课程设计
The left column of WordPress implementation shows the article directory
Market trend report, technical innovation and market forecast of inorganic water treatment chemicals in China
Google Earth engine (GEE) - Global farmland organic soil carbon and nitrogen emissions (1992-2018) data set
EasyExcel-简介-01