日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 第三方模块之 pdfkit

發布時間:2023/12/20 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 pdfkitconfg = 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 TOCReturns: True on success"""

?

?

總結

以上是生活随笔為你收集整理的Python 第三方模块之 pdfkit的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。