Python實(shí)現(xiàn)html轉(zhuǎn)換為pdf報告(生成pdf報告)功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)html轉(zhuǎn)換為pdf報告(生成pdf報告)功能。分享給大家供大家參考,具體如下:
1、先說下html轉(zhuǎn)換為pdf:其實(shí)支持直接生成,有三個函數(shù)pdfkit.f
安裝python包:pip Install pdfkit
系統(tǒng)安裝wkhtmltopdf:參考 https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
mac下的wkhtmltopdf: brew install Caskroom/cask/wkhtmltopdf
import pdfkit pdfkit.from_url('http://google.com','out.pdf') pdfkit.from_file('test.html','out.pdf') pdfkit.from_string('Hello!','out.pdf')
傳遞一個url或者文件名列表:
pdfkit.from_url(['google.com','yandex.ru','engadget.com'],'out.pdf') pdfkit.from_file(['file1.html','file2.html'],'out.pdf')
傳遞一個打開的文件:
withopen('file.html')asf: pdfkit.from_file(f,'out.pdf')
如果你想對生成的PDF作進(jìn)一步處理, 你可以將其讀取到一個變量中:
# 設(shè)置輸出文件為False,將結(jié)果賦給一個變量
pdf=pdfkit.from_url('http://google.com',False)
你可以制定所有的 wkhtmltopdf選項(xiàng) . 你可以移除選項(xiàng)名字前面的 '--' .如果選項(xiàng)沒有值, 使用None, Falseor*作為字典值:
options={ 'page-size':'Letter', 'margin-top':'0.75in', 'margin-right':'0.75in', 'margin-bottom':'0.75in', 'margin-left':'0.75in', 'encoding':"UTF-8", 'no-outline':None } pdfkit.from_url('http://google.com','out.pdf', options=options)
當(dāng)你轉(zhuǎn)換文件、或字符串的時候,你可以通過css選項(xiàng)指定擴(kuò)展的 CSS 文件。
# 單個 CSS 文件 css='example.css'pdfkit.from_file('file.html', options=options, css=css) # Multiple CSS filescss=['example.css','example2.css'] pdfkit.from_file('file.html', options=options, css=css)
你也可以通過你的HTML中的meta tags傳遞任意選項(xiàng):
body = """ <html> <head> <meta name="pdfkit-page-size" content="Legal"/> <meta name="pdfkit-orientation" content="Landscape"/> </head> Hello World! </html> """ pdfkit.from_string(body,'out.pdf')#with --page-size=Legal and --orientation=Landscape
2、再說reporatlab
安裝:
pip install reportlab
簡單使用:
#!/usr/bin/python from reportlab.pdfgen import canvas def hello(): c = canvas.Canvas("helloworld.pdf") c.drawString(100,100,"Hello,World") c.showPage() c.save() hello()
#!/usr/bin/env python import subprocess import datetime from reportlab.pdfgen import canvas from reportlab.lib.units import inch def disk_report(): p = subprocess.Popen("df -h", shell=True, stdout=subprocess.PIPE) # print p.stdout.readlines() return p.stdout.readlines() def create_pdf(input, output="disk_report.pdf"): now = datetime.datetime.today() date = now.strftime("%h %d %Y %H:%M:%S") c = canvas.Canvas(output) textobject = c.beginText() textobject.setTextOrigin(inch, 11*inch) textobject.textLines('''Disk Capcity Report: %s''' %date) for line in input: textobject.textLine(line.strip()) c.drawText(textobject) c.showPage() c.save() report = disk_report() create_pdf(report)
參考:
1、https://github.com/twtrubiks/python-pdfkit-example
2、//chabaoo.cn/article/160638.htm
3、https://bitbucket.org/rptlab/reportlab
4、http://www.reportlab.com/opensource/
5、http://www.reportlab.com/docs/reportlab-userguide.pdf
6、http://chabaoo.cn/article/53233.htm
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python將html轉(zhuǎn)成PDF的實(shí)現(xiàn)代碼(包含中文)
- Python實(shí)現(xiàn)抓取HTML網(wǎng)頁并以PDF文件形式保存的方法
- Python實(shí)現(xiàn)將HTML轉(zhuǎn)成PDF的方法分析
- python 將html轉(zhuǎn)換為pdf的幾種方法
- Python3轉(zhuǎn)換html到pdf的不同解決方案
- python包pdfkit(wkhtmltopdf)?將HTML轉(zhuǎn)換為PDF的操作方法
- Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法
- python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML
- 如何利用Python將html轉(zhuǎn)為pdf、word文件
相關(guān)文章
Python使用百度API上傳文件到百度網(wǎng)盤代碼分享
這篇文章主要介紹了Python使用百度API上傳文件到百度網(wǎng)盤代碼分享,本文使用了一個第三方庫poster,在文中給出了鏈接,需要的朋友可以參考下2014-11-11Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解
今天小編就為大家分享一篇Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python中__slots__節(jié)約內(nèi)存的具體做法
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python中__slots__節(jié)約內(nèi)存的具體做法,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-07-07django API 中接口的互相調(diào)用實(shí)例
這篇文章主要介紹了django API 中接口的互相調(diào)用實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python Opencv實(shí)戰(zhàn)之文字檢測OCR
這篇文章主要為大家詳細(xì)介紹了如何利用Python Opencv實(shí)現(xiàn)文字檢測OCR功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考一下2022-08-08python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改
一個圖片里邊繪制多個圖像是繪圖中的常見需求,下面這篇文章主要給大家介紹了關(guān)于python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05使用Python對接OpenAi?API實(shí)現(xiàn)智能QQ機(jī)器人的方法
這篇文章主要介紹了使用Python對接OpenAi?API實(shí)現(xiàn)智能QQ機(jī)器人的方法,主要是提供一個方法思路,可以根據(jù)實(shí)現(xiàn)代碼延申出更多的解決方法,需要的朋友可以參考下2023-03-03