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"""

?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/453989.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/453989.shtml
英文地址,請注明出處:http://en.pswp.cn/news/453989.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LNMP環境添加第三方模塊

一.在LNMP環境下添加memcache模塊 1.安裝依賴庫(libevent) [rootnode1 ~]# tar xvf libevent-2.0.21-stable.tar.gz [rootnode1 ~]# cd libevent-2.0.21-stable [rootnode1 libevent-2.0.21-stable]# ./configure --prefix/usr/local/libevent [rootnode1 libevent-2.0.21-sta…

學生成績管理系統-程序維護

托管平臺地址&#xff1a;https://gitee.com/lucess/StudentMarkManage.git 小組名稱:干翻沈師 程序運行方法: 1、后臺服務&#xff1a;進入項目文件夾執行 python TeamProject.py runsercer 0.0.0.0:5050 2、前臺服務&#xff1a;進入./WEB-INFO/TeamProjectWeb 文件夾執行 ya…

改需求

轉載于:https://www.cnblogs.com/gw2010/p/7856484.html

架構師一般做到多少歲_軟件測試可以做到多大歲數?

做這個行業也幾年了&#xff0c;經常聽到有人問&#xff0c;軟件測試這個行業能干到多少歲&#xff0c;當然里邊包含想要進入這個行業的和已經在這個行業里邊發展的&#xff01;基本上軟件測試可以分為三條職業發展路線&#xff1a;技術路線、管理路線、產品路線&#xff01;目…

Python 第三方模塊之 MySQL數據庫連接模塊 PyMySQL

PyMySQL的安裝 pip install PyMySQL python連接數據庫 import pymysqlconn pymysql.connect(hostlocalhost, userroot, password"root",databasedb, port3306, # 數字3306charsetutf8, # 不是utf-8autocommitTrue # autocommitTrue 讓每次提交都去調用…

初學Spring Boot

1.Spring Boot注解 (1)SpringBootApplication開啟了Spring的組件掃描和Spring Boot的自動配置,實際上&#xff0c;SpringBootApplication是將三個注解組合在了一起&#xff0c;這三個注解分別是 SpringBootConfiguration&#xff0c;ComponentScan&#xff0c;Ena…

15條常用的視頻音頻編輯腳本命令(mencoder/ffmpeg等)

可以把它當快速簡易參考看&#xff0c;主要的功能有&#xff1a; 視頻格式轉換音頻格式轉換切割視頻及音頻連接兩段視頻視頻音頻同步將圖像系列轉換成視頻 這里是百鬼丸以前收集的一部分命令行視頻音頻編輯腳本命令&#xff0c;一直在自己的記事本里隨時用&#xff0c;現在…

python rowcount_PyQt(Python+Qt)學習隨筆:QTableWidget的currentItem、rowCount、columnCount等部件狀態屬性訪問方法...

老猿將QTableWidget表格部件中反映部件當前情況的一些方法歸類為部件狀態訪問方法&#xff0c;包括部件的行數、列數、當前項、當前行、當前列等屬性訪問方法。1、行數rowCountQTableWidget的rowCount屬性保存表格部件中的行數&#xff0c;在QTableWidget創建時如果沒有指定行數…

Python 內置模塊之 random

常用API import random# 隨機小數 print(random.random()) # 大于0且小于1之間的小數。0< n<1.0 print(random.uniform(1,3)) # 大于1小于3的小數# 隨機整數 print(random.randint(1,5)) # 大于等于1且小于等于5之間的整數#從指定范圍內&#xff0c;按指定基…

微信jssdk遇到的一些問題匯總

1.用戶手動去觸發的接口可以直接調用比如wx.startRecord(); 但是寫在頁面加載完成里就無效&#xff0c;需要寫在 wx.ready(function(){wx.startRecord(); }); 才會有效。 2.h5 的audio標簽只支持ogg,mp3,wav格式的音頻&#xff0c;微信jssdk錄制的是amr格式的語音文件&#xf…

mongodb簡單的增刪改查

數據庫操作&#xff1a; show dbs;#查看數據庫use test;#如果沒有就創建一個db;#查看當前數據庫db.dropDatabase();#刪除數據庫 數據操作&#xff1a;show collections&#xff1b;#查看集合創建集合、插入&#xff1a;create collection;#創建集合db.student.insert({"na…

ffmpeg-0.8 開源編碼解碼庫從linux下移植到windows vs2005

最新 ffmpeg-0.8 開源編碼解碼庫&#xff0c;從linux下移植到windows vs2005&#xff0c;全部開源。需要 Intel C Compile 和 開源的SDL庫支持&#xff0c;由于 Intel C Compile支持C99語法&#xff0c;所以源代碼改動很小很小。主要的修改1&#xff1a;添加了linux中有而windo…

python3.5.2使用教程_Python3.5.2-初級教程.docx

Python3.5.2-初級教程Python 初級教程Release:3.5.2引言Python 是一門簡單易學且功能強大的編程語言。它擁有高效的高級數據結構&#xff0c;并且能夠用簡單而又高效的方式進行面向對象編程。Python 優雅的語法和動態類型&#xff0c;再結合它的解釋性&#xff0c;使其在大多數…

Flask 離線腳本

1. 在 __init__.py中創建db對象from flask_sqlalchemy import SQLAlchemy# 包含了SQLAlchemy相關的所有操作db SQLAlchemy()2. 在 __init__.py中create_app函數中讓將app傳入到db中def create_app():app Flask(__name__)app.config.from_object(settings.DevelopmentConfig)f…

day13 迭代器和生成器

一、上節回顧和作業講解&#xff1a; 1、如果這個網頁沒有被爬取過就真的去訪問這個網頁&#xff0c;否則就返回之前訪問的時候緩存文件中的內容 &#xff08;重要的例子&#xff09; from urllib.request import urlopen def wrapper(func):def inner(*args, **kwargs):with o…

Centos7.0 搭建Zabbix環境

實驗環境&#xff1a;Centos7.0IP:192.168.47.140關閉iptables及setenforce導入源 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm安裝zabbix包安裝完成安裝mysql源wget http://repo.mysql.com/mysql-community-release-el7-5…

用線性插值算法實現圖像縮放

用線性插值算法實現圖像縮放 猛禽[Mental Studio](個人專欄)(BLOG) http://mental.mentsu.com 在Windows中做過圖像方面程序的人應該都知道Windows的GDI有一個API函數&#xff1a;StretchBlt&#xff0c;對應在VCL中是TCanvas類的StretchDraw方法。它可以很簡單地實現圖像的縮放…

蒙特卡洛分析 pmp_PMP基礎名詞介紹 | 59. 實施定量風險分析

點擊上方藍字關注我們你好&#xff0c;這是“兔子研習社”為管理新手推出的“PMP基礎名詞介紹”系列內容。如果你正打算轉到管理崗位&#xff0c;或者想要學習國際通行的項目管理知識&#xff0c;那恭喜你&#xff0c;這里滿滿的干貨會讓你不虛此行。實施定量風險分析是就已識別…

深度學習案例之基于 CNN 的 MNIST 手寫數字識別

一、模型結構 本文只涉及利用Tensorflow實現CNN的手寫數字識別,CNN的內容請參考:卷積神經網絡(CNN) MNIST數據集的格式與數據預處理代碼input_data.py的講解請參考 :Tutorial (2) 二、實驗代碼 # -*- coding:utf-8 -*- """Time : Author: Feng LepengFile …

怎樣獲取linux命令幫助?

獲得命令使用幫助&#xff1a;內部命令&#xff1a;help COMMAND外部命令&#xff1a;COMMAND --help &#xff08;大多數命令有help選項&#xff09;命令手冊&#xff1a;manualman [章節號] COMMAND其中man數據庫是分章節的&#xff0c;相同的COMMAND出現在不同的章節表示…