日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python pdf报告_Python实现html转换为pdf报告(生成pdf报告)功能示例

發布時間:2025/4/5 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python pdf报告_Python实现html转换为pdf报告(生成pdf报告)功能示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了Python實現html轉換為pdf報告(生成pdf報告)功能。分享給大家供大家參考,具體如下:

1、先說下html轉換為pdf:其實支持直接生成,有三個函數pdfkit.f

安裝python包:pip Install pdfkit

系統安裝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作進一步處理, 你可以將其讀取到一個變量中:

# 設置輸出文件為False,將結果賦給一個變量

pdf=pdfkit.from_url('http://google.com',False)

你可以制定所有的 wkhtmltopdf選項 . 你可以移除選項名字前面的 '--' .如果選項沒有值, 使用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)

當你轉換文件、或字符串的時候,你可以通過css選項指定擴展的 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傳遞任意選項:

body = """

Hello World! """

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、//www.jb51.net/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、https://www.jb51.net/article/53233.htm

更多Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

總結

以上是生活随笔為你收集整理的python pdf报告_Python实现html转换为pdf报告(生成pdf报告)功能示例的全部內容,希望文章能夠幫你解決所遇到的問題。

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