Python 第三方模块之 pdfkit

pdfkit,把 HTML+CSS 格式的文件转换成 PDF 格式文档的一个工具。

其实,pdfkit 是 html 转成 pdf 工具包 wkhtmltopdf 的 Python 封装。所以,首先安装 wkhtmltopdf 。 一般情况下,wkhtmltopdf需要手动安装,网站是 https://wkhtmltopdf.org/downloads.html,根据自己的操作系统下载对应的版本即可。ps:记住安装目录啊,下面要用到。

上面说到了pdfkit这个模块,这个是第三方模块,需要安装,使用pip安装即可。

pip install pdfkit

示例

pdfkit 可以将网页、html文件以及字符串生成pdf文件

 

import pdfkit
 

confg = pdfkit.configuration(wkhtmltopdf='C:\Python35\wkhtmltopdf.exe')# 这里指定一下wkhtmltopdf的路径,这就是我为啥在前面让记住这个路径
url = 'https://blog.csdn.net/fenglepeng/article/details/103670893'
pdfkit.from_url(url, 'aaa.pdf', configuration=confg)
# from_url这个函数是从url里面获取内容
# 这有3个参数,第一个是url,第二个是文件名,第三个就是khtmltopdf的路径
 
pdfkit.from_file('my.html', 'bbb.pdf', configuration=confg)
# from_file这个函数是从文件里面获取内容
# 这有3个参数,第一个是一个html文件,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径
 
html = '''
<div>
<h1>title</h1>
<p>content</p>
</div>
'''
pdfkit.from_string(html, 'ccc.pdf', configuration=confg)
# from_file这个函数是从一个字符串里面获取内容
# 这有3个参数,第一个是一个字符串,第二个是文生成的pdf的名字,第三个就是khtmltopdf的路径

API

def from_url(url, output_path, options=None, toc=None, cover=None, configuration=None, cover_first=False): 
    """ 
    把从URL获取文件转换为PDF文件 
    :param url: URL 或 URL列表 
    :param output_path: 输出PDF文件的路径。如果是参数等于False,意味着文件将会以字符串的形式返回,得到文本文件。
    :param options: (可选) dict with wkhtmltopdf global and page options, with or w/o '--' 
    :param toc: (可选) dict with toc-specific wkhtmltopdf options, with or w/o '--' 
    :param cover: (可选) string with url/filename with a cover html page 
    :param configuration: (可选)实例化 pdfkit.configuration.Configuration() 
    :param configuration_first: (可选) if True, cover always precedes TOC 
    Returns:成功返回True 
    """ 


def from_file(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False): 
    """ 
    Convert HTML file or files to PDF document 
    :param input: path to HTML file or list with paths or file-like object  
    :param output_path: path to output PDF file. False means file will be returned as string. 
    :param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
    :param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' 
    :param cover: (optional) string with url/filename with a cover html page 
    :param css: (optional) string with path to css file which will be added to a single input file 
    :param configuration: (optional) instance of pdfkit.configuration.Configuration() 
    :param configuration_first: (optional) if True, cover always precedes TOC 
    Returns: True on success 
    """ 


def from_string(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False):
    """
    Convert given string or strings to PDF document
    :param input: string with a desired text. Could be a raw text or a html file
    :param output_path: path to output PDF file. False means file will be returned as string.
    :param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
    :param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
    :param cover: (optional) string with url/filename with a cover html page
    :param css: (optional) string with path to css file which will be added to a input string
    :param configuration: (optional) instance of pdfkit.configuration.Configuration()
    :param configuration_first: (optional) if True, cover always precedes TOC
    Returns: True on success
    """

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python第三方模块是由Python社区或其他开发者开发并维护的可复用软件包。这些模块提供了各种功能,例如网络编程、图形用户界面、数据处理、科学计算、机器学习等等。以下是一些常用的Python第三方模块: 1. NumPy:用于科学计算的基础模块,提供了高效的多维数组操作和数学函数库。 2. Pandas:用于数据处理和分析的模块,提供了数据结构和函数库,可以轻松地处理不同类型的数据。 3. Matplotlib:用于数据可视化的模块,提供了各种绘图函数和样式选项,可以生成高质量的图表和图形。 4. Scikit-learn:用于机器学习和数据挖掘的模块,提供了各种分类、回归、聚类和降维算法,以及数据预处理和模型评估函数。 5. Requests:用于网络编程的模块,提供了简单易用的HTTP请求函数,可以下载网页、API数据和其他资源。 6. Flask:用于Web开发的微框架,提供了简单易用的路由、模板和扩展机制,可以快速开发Web应用。 7. Pygame:用于游戏开发的模块,提供了音频、图像和用户输入等功能,可以创建2D游戏和动画效果。 8. Django:用于Web开发的全功能框架,提供了ORM、模板、表单和管理后台等功能,可以创建大型Web应用。 9. TensorFlow:用于深度学习和人工智能的模块,提供了高效的计算图和优化算法,可以训练和部署复杂的神经网络模型。 10. OpenCV:用于计算机视觉和图像处理的模块,提供了各种图像处理函数和算法,可以实现目标检测、图像分割和人脸识别等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值