python reportlab 生成table
'''
Table(data, colWidths=None, rowHeights=None, style=None, splitByRow=,
repeatRows=, repeatCols=, rowSplitRange=None, spaceBefore=None,
spaceAfter=None) '''
'''
Table and Tablestyle
TableStyle user Methods
.TableStyle(commandSequence)
The creation method initializes the TableStyle with the argument command sequence
eg: LIST_STYLE = TableStyle(
[('LINEABOVE', (,), (-,), , colors.green),
('LINEABOVE', (,), (-,-), 0.25, colors.black),
('LINEBELOW', (,-), (-,-), , colors.green),
('ALIGN', (,), (-,-), 'RIGHT')] . TableStyle.add(commandSequence)
This method allows you to add commands to an existing TableStyle, i.e. you can build up
TableStyles in multiple statements.
eg:
LIST_STYLE.add('BACKGROUND', (,), (-,), colors.Color(,0.7,0.7))
.TableStyle.getCommands()
This method returns the sequence of commands of the instance.
cmds = LIST_STYLE.getCommands() .TableStyle Commands
TableStyle Cell Formatting Commands FONT - takes fontname, optional fontsize and optional leading.
FONTNAME (or FACE) - takes fontname.
FONTSIZE (or SIZE)- takes fontsize in points; leading may get out of sync.
LEADING- takes leading in points.
TEXTCOLOR- takes a color name or (R,G,B) tuple.
ALIGNMENT (or ALIGN)- takes one of LEFT, RIGHT and CENTRE (or CENTER) or DECIMAL.
LEFTPADDING- takes an integer, defaults to .
RIGHTPADDING- takes an integer, defaults to .
BOTTOMPADDING- takes an integer, defaults to .
TOPPADDING- takes an integer, defaults to .
BACKGROUND- takes a color defined by an object, string name or numeric tuple/list,
or takes a list/tuple describing a desired gradient fill which should
contain three elements of the form [DIRECTION, startColor, endColor]
where DIRECTION is either VERTICAL or HORIZONTAL.
ROWBACKGROUNDS- takes a list of colors to be used cyclically.
COLBACKGROUNDS- takes a list of colors to be used cyclically.
VALIGN- takes one of TOP, MIDDLE or the default BOTTOM TableStyle Line Commands
Line commands begin with the identifier, the start and stop cell coordinates and always follow this with the thickness
(in points) and color of the desired lines. Colors can be names, or they can be specified as a (R, G, B) tuple, where
R, G and B are floats and (, , ) is black. The line command names are: GRID, BOX, OUT- LINE, INNERGRID, LINEBELOW,
LINEABOVE, LINEBEFORE and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX
and INNERGRID. #TableStyle Span Commands
Our Table classes support the concept of spanning, but it isn't specified in the same way as html. The style
specification
SPAN, (sc,sr), (ec,er)
indicates that the cells in columns sc - ec and rows sr - er should be combined into a super cell with con- tents
determined by the cell (sc, sr). The other cells should be present, but should contain empty strings or you may
get unexpected results. '''
# example
from reportlab.lib.units import inch
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont('hei', 'SIMHEI.TTF'))
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
import time elements = [] # TableStyle Commands
# BACKGROUND, and TEXTCOLOR commands
data = [['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']]
t = Table(data,colWidths=[, ,,,])
t.setStyle(TableStyle([('BACKGROUND', (, ), (-, -), colors.green),
('TEXTCOLOR', (, ), (, -), colors.red)])) elements.append(t) data = [['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']]
t = Table(data,colWidths=[, ,,,],
style=[('GRID', (, ), (-, -), , colors.green),
('BOX', (, ), (, -), , colors.red),
('LINEABOVE', (, ), (-, ), , colors.blue),
('LINEBEFORE', (, ), (, -), , colors.pink),
]) elements.append(t) data = [['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']]
t = Table(data, * [0.4 * inch], * [0.4 * inch])
t.setStyle(TableStyle([('ALIGN', (, ), (-, -), 'RIGHT'),
('TEXTCOLOR', (, ), (-, -), colors.red),
('VALIGN', (, ), (, -), 'TOP'),
('TEXTCOLOR', (, ), (, -), colors.blue),
('ALIGN', (, -), (-, -), 'CENTER'),
('VALIGN', (, -), (-, -), 'MIDDLE'),
('TEXTCOLOR', (, -), (-, -), colors.green),
('INNERGRID', (, ), (-, -), 0.25, colors.black),
('BOX', (, ), (-, -), 0.25, colors.black),
])) elements.append(t)
# print(elements) # TableStyle Line Commands data = [['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']]
t = Table(data, style=[('GRID', (, ), (-, -), , colors.green),
('BOX', (, ), (, -), , colors.red),
('LINEABOVE', (, ), (-, ), , colors.blue),
('LINEBEFORE', (, ), (, -), , colors.pink),
]) elements.append(t) data = [['', '', '闪电', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']]
t = Table(data, style=[
('FONTNAME', (, ), (-, -), 'hei'),
('GRID', (, ), (-, -), 0.5, colors.grey),
('GRID', (, ), (-, -), , colors.green),
('BOX', (, ), (, -), , colors.red),
('BOX', (, ), (-, -), , colors.black),
('LINEABOVE', (, ), (-, ), , colors.blue),
('LINEBEFORE', (, ), (, -), , colors.pink),
('BACKGROUND', (, ), (, ), colors.pink),
('BACKGROUND', (, ), (, ), colors.lavender),
('BACKGROUND', (, ), (, ), colors.orange),
]) elements.append(t) # TableStyle Span Commands data = [['Top\nLeft', '', '', '', ''],
['', '', '', '', ''],
['', '', '', 'Bottom\nRight', ''],
['', '', '', '', '']]
T = Table(data, style=[
('GRID', (, ), (-, -), 0.5, colors.grey),
('BACKGROUND', (, ), (, ), colors.palegreen),
('SPAN', (, ), (, )),
('BACKGROUND', (-, -), (-, -), colors.pink),
('SPAN', (-, -), (-, -)),
]) print(elements)
doc = SimpleDocTemplate('demo5.pdf')
doc.build(elements)
python reportlab 生成table的更多相关文章
- python之reportlab生成PDF文件
项目需要,需要自动生成PDF测试报告.经过对比之后,选择使用了reportlab模块. 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成 ...
- Python之将Python字符串生成PDF
笔者在今天的工作中,遇到了一个需求,那就是如何将Python字符串生成PDF.比如,需要把Python字符串'这是测试文件'生成为PDF, 该PDF中含有文字'这是测试文件'. 经过一番检索, ...
- python随机生成个人信息
python随机生成个人信息 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import sys import random class Personal ...
- python快速生成注释文档的方法
python快速生成注释文档的方法 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等.一看别人专业的大牛们写的文档多牛多羡慕,不用担 ...
- 利用Python自动生成暴力破解的字典
Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...
- 动态生成Table内文字换行。
后台动态生成table,并把td内的文字进行换行. 前台: <body style="width:100%;height:540px;margin-left:0px;margin-to ...
- Python解析生成XML-ElementTree VS minidom
OS:Windows 7 关键字:Python3.4,XML,ElementTree,minidom 本文介绍用Python解析生成以下XML: <Persons> <Person& ...
- 从用python自动生成.h的头文件集合和类声明集合到用python读写文件
最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些 ...
- Python随机生成验证码的两种方法
Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...
随机推荐
- 为Python配置Vim编辑器(GUI/非GUI皆可)
原文地址:https://blog.csdn.net/alanzjl/article/details/49383943 Vim as a python IDE ** 最近一直在写Python,但一直没 ...
- meshing-simple_block
原视频下载地址:https://yunpan.cn/cqjeSzP7s93Pc 访问密码 aaff
- Vue路由管理之Vue-router
一.Vue Router介绍 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的. ...
- Alpha项目冲刺! Day4-产出
各个成员今日完成的任务 林恩:任务分工,博客撰写,了解安卓环境搭建 杨长元:安卓本地数据库基本建立 李震:学习 胡彤:完善服务端 寇永明:学习 王浩:学习 李杰:学习 各个成员遇到的问题 林恩:为自己 ...
- TynSerial结构体序列(还原)
TynSerial结构体序列(还原) 1)定义一个结构体 type TRec = record id, name: string; end; 2)结构体序列(还原) procedure TForm1. ...
- 快速安装python3
使用 rpm 包进行安装 先来介绍一下 IUS 这个社区,名字的全写是[Inline with Upstream Stable]取首字母,它主要是一个提供新版本RPM包的社区.具体使用可以查看官方文档 ...
- 你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
- eclipse手动添加本地jar包到本地maven仓库
在使用maven进行构建项目时,有时候中央仓库不包含所需的jar包,就需要下载到本地后手动添加到本地仓库中.这里介绍下利用eclipse进行本地jar安装到maven本地仓库. 在Eclipse项目中 ...
- ElementUI】日期选择器时间选择范围限制,只能选今天之前的时间,或者是只能选今天之后的时间。今天是否可以选。限制结束日期不能大于开始日期
<el-date-picker v-model="value1" type="date" placeholder="选择日期" :pi ...
- JavaScript箭头函数(Lambda表达式)
箭头函数也叫lambda表达式 据说其主要意图是定义轻量级的内联回调函数 栗有: 1 var arr = ["wei","ze","yang" ...