网站首页 > 知识剖析 正文
在用jupyter notebook写代码文档的时候,有时需要导出pdf版本,但jupyter会报错。我在想,除了网上的debug方法,还没有其他方案可以生成pdf。
度娘搜了下,很多博客推荐Python的第三方库pdfkit,可以将网页、html文件以及字符串生成pdf文件。
其实也有很多软件提供pdf生成服务,但这样太不python了,那下面就来试试pdfkit怎么用吧!
三步实现自动生成pdf文档:
1.使用pip安装pdfkit库
python版本 3.x,在命令行输入:
pip install pdfkit
安装过程基本不会有啥问题,出现上面的Successfully installed pdfkit-0.6.1提示,说明安装成功了。
2.安装wkhtmltopdf.exe文件
注:pdfkit是基于wkhtmltopdf的python封装,所以需要安装wkhtmltopdf.exe。wkhtmltopdf是轻量级软件,非常很容易安装。
下载地址
:https://wkhtmltopdf.org/downloads.html
下载wkhtmltopdf
下载完成后,一路next,将wkhtmltopdf安装好。
务必要记住安装地址,找到wkhtmltopdf.exe文件所在的绝对路径,后面要用到。
我这里是默认路径""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe""
安装wkhtmltopdf
3.使用pdfkit库生成pdf文件
前面说过pdfkit可以将网页、html文件、字符串生成pdf文件。
- 网页url生成pdf【pdfkit.from_url()函数】
# 导入库
import pdfkit
'''将网页url生成pdf文件'''
def url_to_pdf(url, to_file):
# 将wkhtmltopdf.exe程序绝对路径传入config对象
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# 生成pdf文件,to_file为文件路径
pdfkit.from_url(url, to_file, configuration=config)
print('完成')
# 这里传入我知乎专栏文章url,转换为pdf
url_to_pdf(r'https://zhuanlan.zhihu.com/p/69869004', 'out_1.pdf')
- html文件生成pdf【pdfkit.from_file()函数】
# 导入库
import pdfkit
'''将html文件生成pdf文件'''
def html_to_pdf(html, to_file):
# 将wkhtmltopdf.exe程序绝对路径传入config对象
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# 生成pdf文件,to_file为文件路径
pdfkit.from_file(html, to_file, configuration=config)
print('完成')
html_to_pdf('sample.html','out_2.pdf')
- 字符串生成pdf【pdfkit.from_string()函数】
# 导入库
import pdfkit
'''将字符串生成pdf文件'''
def str_to_pdf(string, to_file):
# 将wkhtmltopdf.exe程序绝对路径传入config对象
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# 生成pdf文件,to_file为文件路径
pdfkit.from_string(string, to_file, configuration=config)
print('完成')
str_to_pdf('This is test!','out_3.pdf')
4.结论
本文讲了如何在Python中使用pdfkit库生成pdf文件,非常方便快捷,适合批量自动化操作。
我们看看生成的pdf效果如何:
pdf效果展示
整体页面视觉不错呦,赶快用起来吧!
- 上一篇: 《逆水寒》:全面攻略详细解析
- 下一篇: 蠕动泵TH10泵头流量参数
猜你喜欢
- 2025-03-06 别看它又小又少,黄豆大小,人体只有4颗,如果出了问题,就会...
- 2025-03-06 #天真不能无鞋#记个流水账的鞋:二十余双鞋子简评
- 2025-03-06 javascript实现获取中文汉字拼音首字母
- 2025-03-06 少年三国志零兑换码大全 少年三国志2礼包兑换码怎么用
- 2025-03-06 第32批免征车辆购置税新能源汽车车型目录公布
- 2025-03-06 肾病化验指标不会看?直接图表讲解告诉你,简单易懂!快收藏
- 2025-03-06 蠕动泵TH10泵头流量参数
- 2025-03-06 《逆水寒》:全面攻略详细解析
- 2025-03-06 nginx实现公告维护页面
- 2025-03-06 PDF编辑与THML编辑器支持pdf转html、html转pdf、pdf转图片
- 05-05vin码怎么查车型?车辆VIN码的第十位代表什么信息?
- 05-05Java数组数据的操作之检查日期格式是否正确
- 05-05苹果序列号怎么看生产日期和产地?
- 05-05参考文献中的M J N D字母代表什么?
- 05-05闲鱼交易技巧,满满的干货(闲鱼的交易流程怎么样的?我是买家)
- 05-05忘记自已多少岁了?可试试年龄计算器
- 05-05汽车-剖析、解析车架号(VIN)中的第10位-车型年份
- 05-05干货|史上最全波特酒年份指南(波特酒 年份)
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)