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数据结构学习
列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能. 以下是 Python 中列表的方法: 方法 描述 list.append(x ...
- Hibernate用到HQL查询时的错误
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: student is ...
- Android Studio如何删除一个Module
当你想在Android Studio中删除某个module时,大家习惯性的做法都是选中要删除的module,右键去找delete.但是在Android Studio中你选中module,右键会发现没 ...
- zabbix (二)安装
一.centos7源码安装zabbix3.x 1.安装前环境搭建 下载最新的yum源 #wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/ ...
- C语言的历史
1.ALGOL语言 ALGOL ,为算法语言(ALGOrithmic Language)的缩写,是计算机发展史上首批产生的高级程式语言家族.当时还是晶体管计算机流行的时代,由于ALGOL语句和普通语言 ...
- Apache日志详解
在渗透测试的工作中,WEB网站的日志是非常重要的,今天总结了一些关于调配Apache日志的一些东西. 0x00 Apache日志文件名称及路径介绍 我们安装好Apache后,Apache的配置文件(h ...
- Nginx之URL重写(rewrite)配置
Nginx URL重写(rewrite)配置及信息详解1)if判断指令 语法为if(condition){…} #对给定的条件condition进行判断.如果为真,大括号内的rewrite指令 ...
- Debian/Ubuntu/CentOS开机启动
说明:常用的Linux启动项就是在/etc/rc.local的exit 0语句之间添加启动脚本,另一种方法,使用update-rc.d命令添加/禁止开机启动项. 在centos7中增加脚本有两种常用的 ...
- php 验证中文和部分自定义符号
$str = '54787dDp中s-:"'; $rule ="/^[\x{4e00}-\x{9fa5}A-Za-z0-9`·!!@#$\¥%…^&*(())\-+=“.\ ...
- 初识kaggle,以及记录 kaggle的使用
1.简介:Kaggle是一个数据建模和数据分析竞赛的平台.企业和研究者可在其上发布数据,统计学者和数据挖掘专家可在其上进行竞赛,通过“众包”的形式以产生最好的模型.Kaggle可以分为Competit ...