23

pdfkit | 自动化利器,生成PDF就靠它了

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA%3D%3D&%3Bmid=2650072446&%3Bidx=3&%3Bsn=91d810356df8f3bc9a2ed27bbc22f16e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在用jupyter notebook写代码文档的时候,有时需要导出pdf版本,但jupyter会报错。我在想,除了网上的debug方法,还没有其他方案可以生成pdf。

度娘搜了下,很多博客推荐Python的第三方库 pdfkit ,可以将 网页、html文件以及字符串 生成pdf文件。

其实也有很多软件提供pdf生成服务,但这样太不python了,那下面就来试试 pdfkit 怎么用吧!

三步实现自动生成pdf文档:

  1. 使用 pip 安装 pdfkit

python版本 3.x,在命令行输入:

pip install pdfkit

2UzYJzV.png!web

安装过程基本不会有啥问题,出现上面的 Successfully installed pdfkit-0.6.1 提示,说明安装成功了。

  1. 安装 wkhtmltopdf.exe 文件

注:pdfkit是基于wkhtmltopdf的python封装,所以需要安装wkhtmltopdf.exe。wkhtmltopdf是轻量级软件,非常很容易安装。

下载地址:

https://wkhtmltopdf.org/downloads.html

nymMN3f.png!web 下载wkhtmltopdf

下载完成后,一路next,将wkhtmltopdf安装好。

务必要记住安装地址,找到 wkhtmltopdf.exe 文件所在的绝对路径,后面要用到。

我这里是默认路径 ""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe""

zieI73v.png!web 安装wkhtmltopdf
  1. 使用 pdfkit 库生成pdf文件

前面说过 pdfkit 可以将 网页、html文件、字符串 生成pdf文件。

  • 网页生成pdf【 pdfkit.from_url() 函数】
# 导入库
import pdfkit

'''将网页生成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')
  1. 结论

本文讲了如何在Python中使用 pdfkit 库生成pdf文件,非常方便快捷,适合批量自动化操作。

我们看看生成的pdf效果如何:

AvINnyF.jpg!web pdf效果展示

整体页面视觉不错呦,赶快用起来吧!

精彩回顾

Python网络爬虫与文本数据分析

如何使用Python快速构建领域内情感词典

Pandas 50题练习

Pandas数据处理——盘点那些常用的函数(上)

Pandas数据处理——盘点那些常用的函数(下)

中文文本数据逻辑性分析库

中文文本分析相关资源汇总

cnsenti中文情绪情感分析库

Python全栈-60天精通之路

Python数据分析相关学习资源汇总帖

漂亮~pandas可以无缝衔接Bokeh

综述:文本分析在市场营销研究中的应用

2020年B站跨年晚会弹幕内容分析

YelpDaset: 酒店管理类数据集10+G

Loughran&McDonald金融文本情感分析库

测试代码获取,在后台留言:pdfkit


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK