When writing code documents in jupyter notebook, sometimes you need to export the pdf version, but jupyter will report an error. I was thinking that apart from the online debug method, there is no other solution to generate pdf.
Du Niang searched, and many blogs recommend Python's third-party library pdfkit
, which can generate pdf files from webpages, html files and strings.
In fact, there are many softwares that provide pdf generation services, but this is too pythonic, so let's try how to use pdfkit
!
pip
to install the pdfkit
libraryPython version 3.x, type in the command line:
pip install pdfkit
There is basically no problem in the installation process. The above Successfully installed pdfkit-0.6.1
prompt appears, indicating that the installation is successful.
wkhtmltopdf.exe
fileNote: pdfkit is a python package based on wkhtmltopdf, so wkhtmltopdf.exe needs to be installed. wkhtmltopdf is a lightweight software, very easy to install.
download link:
https://wkhtmltopdf.org/downloads.html
Download wkhtmltopdf
After the download is complete, go next all the way and install wkhtmltopdf.
Be sure to remember the installation address, find the absolute path where the wkhtmltopdf.exe
file is located, and use it later.
My default path here is ""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe""
Install wkhtmltopdf
pdfkit
library to generate pdf filesAs mentioned earlier, pdfkit
can generate pdf files from webpages, html files, and strings.
pdfkit.from_url()
function]# Import library
import pdfkit
''' Generate pdf files from web pages'''
def url_to_pdf(url, to_file):
# Add wkhtmltopdf.The absolute path of the exe program is passed into the config object
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# Generate pdf file, to_file is the file path
pdfkit.from_url(url, to_file, configuration=config)print('carry out')
# Pass in here I know the column url and convert it to pdf
url_to_pdf(r'https://zhuanlan.zhihu.com/p/69869004','out_1.pdf')
pdfkit.from_file()
function]# Import library
import pdfkit
''' Generate pdf file from html file'''
def html_to_pdf(html, to_file):
# Add wkhtmltopdf.The absolute path of the exe program is passed into the config object
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# Generate pdf file, to_file is the file path
pdfkit.from_file(html, to_file, configuration=config)print('carry out')html_to_pdf('sample.html','out_2.pdf')
pdfkit.from_string()
function]# Import library
import pdfkit
''' Generate a pdf file from the string'''
def str_to_pdf(string, to_file):
# Add wkhtmltopdf.The absolute path of the exe program is passed into the config object
path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# Generate pdf file, to_file is the file path
pdfkit.from_string(string, to_file, configuration=config)print('carry out')str_to_pdf('This is test!','out_3.pdf')
This article talks about how to use the pdfkit
library to generate pdf files in Python, which is very convenient and fast, suitable for batch automation.
Let's see how the generated pdf effect:
pdf effect display
The overall page has a good visual look, so hurry up and use it!
Recommended Posts