Python实现html转换为pdf报告(生成pdf报告)功能示例
作者:脚本小娃子 发布时间:2023-11-07 02:31:17
本文实例讲述了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 = """ <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、//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程序设计有所帮助。
来源:https://www.cnblogs.com/shengulong/p/7994082.html


猜你喜欢
- 目录前言创建对象方式一:方式二:更新对象方式一:方式二:方式三:查询检索全部对象:条件过滤:方式一:方式二:检索单个对象:总结前言上篇已经介
- 大家好,由于公司忙着赶项目,导致有段时间没有发布新文章了。今天我想跟大家谈谈Cookie的使用。同样,这个Cookie的使用方法是我从公司的
- 前言学python对selenium应该不陌生吧Selenium 是最广泛使用的开源 Web UI(用户界面)自动化测试套件之一。Selen
- 国外的空间和我们国内的空间使用的语言系统一般不一样,所以在网页程序上时如果处理不当很容易出现乱码,看了让人摸不着头脑。所以我们在编写程序时就
- 什么是浮动?浮动是 css 的定位属性。我们可以看一下印刷设计来了解它的起源和作用。印刷布局中,文本可以按照需要围绕图片。一般把这种方式称为
- 本文系统的对HTTP Headers进行了简明易懂的阐述,我仅稍作笔记。什么是HTTP HeadersHTTP是“Hypertext Tra
- 简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁"
- LyScript 可实现自定义汇编指令的替换功能,用户可以自行编写一段汇编指令,将程序 * 定的通用函数进行功能改写与转向操作,此功能原理是简
- 如何使用,直接上代码/** * 安装node-xlsx插件 */var path = require('path')var
- 问题我使用python 2.7和xlwt模块进行excel导出我想设置我知道可以使用的单元格的背景颜色style1 = xlwt.easyx
- 题目:来自Madrid且订单数少于3的消费者 建表:set nocount on --当 SET NOCOUNT 为
- mysql实现sequence功能1.建立sequence记录表CREATE TABLE `sys_sequence` ( `seq_nam
- 与 Python 一样,Go 语言也有空白标识符。1.什么是空白标识符空白标识符是未使用的值的占位符。它由下划线(_)表示。由于空白标识符没
- 本文实例讲述了Python Excel表格创建乘法表。分享给大家供大家参考,具体如下:题目如下:创建程序multiplicationTabl
- 一、 简单查询简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的表或视图、以及搜索
- 为什么要问如何存储IP?首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗?其实做任何程序设计都要在功能
- 本文实例讲述了JS实现利用两个队列表示一个栈的方法。分享给大家供大家参考,具体如下:先看原理图:理清楚思路,再动笔写:<!DOCTYP
- 挑钻石第二弹seaborn是matplotlib的补充包,提供了一系列高颜值的figure,并且集成了多种在线数据集,通过sns.load_
- 本文实例讲述了JavaScript+canvas实现七色板效果。分享给大家供大家参考,具体如下:效果图如下:html:<canvas
- 分页一般和表格一起用,分页链接作为表格的一部分,将分页链接封装成一个独立的组件,然后作为子组件嵌入到表格组件中,这样比较合理。效果:代码:1