'''
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的更多相关文章

  1. python之reportlab生成PDF文件

    项目需要,需要自动生成PDF测试报告.经过对比之后,选择使用了reportlab模块. 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成 ...

  2. Python之将Python字符串生成PDF

      笔者在今天的工作中,遇到了一个需求,那就是如何将Python字符串生成PDF.比如,需要把Python字符串'这是测试文件'生成为PDF, 该PDF中含有文字'这是测试文件'.   经过一番检索, ...

  3. python随机生成个人信息

    python随机生成个人信息 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import sys import random class Personal ...

  4. python快速生成注释文档的方法

    python快速生成注释文档的方法 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等.一看别人专业的大牛们写的文档多牛多羡慕,不用担 ...

  5. 利用Python自动生成暴力破解的字典

    Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...

  6. 动态生成Table内文字换行。

    后台动态生成table,并把td内的文字进行换行. 前台: <body style="width:100%;height:540px;margin-left:0px;margin-to ...

  7. Python解析生成XML-ElementTree VS minidom

    OS:Windows 7 关键字:Python3.4,XML,ElementTree,minidom 本文介绍用Python解析生成以下XML: <Persons> <Person& ...

  8. 从用python自动生成.h的头文件集合和类声明集合到用python读写文件

    最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些 ...

  9. Python随机生成验证码的两种方法

    Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...

随机推荐

  1. MQ Cannot convert from [[B] to [] for GenericMessage

    MQ消费端转换报错:主要错误信息:Caused by: org.springframework.messaging.converter.MessageConversionException: Cann ...

  2. nginx 转发 header 数据丢失

    刚帮同事解决了个问题,记录一下,现象:放在header里面的数据,本地后台可以收到,集成可以收到,测试不行, 查看代码没问题,排除代码问题,比较集成和测试环境有何不同,发现集成环境是局域网访问,192 ...

  3. 安装virtual box

    将安装包放在app目录下: 进入安装目录,输入sudo gdebi 安装包名 安装完后,可以在搜索框中搜索:virtual 会出现安装好的虚拟机盒子.

  4. flutter doctor出现问题 [!] Android toolchain - develop for Android devices (Android SDK version 28.0.3) X Android license status unknown. Try re-installing or updating your Android SDK Manager. 的解决方案

    首先,问题描述: flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Cha ...

  5. chrome dev

    chrome://plugins 为什么无法打开? Chrome插件问答 2018-03-02 13:34     最后又很多网友在我们 chrome插件 网反应说chrome://plugins 无 ...

  6. 各种Android UI开源框架 开源库

    各种Android UI开源框架 开源库 转 https://blog.csdn.net/zhangdi_gdk2016/article/details/84643668 自己总结的Android开源 ...

  7. Python Re 模块超全解读

    re模块下的函数 compile(pattern):创建模式对象 import re pat=re.compile('A') m=pat.search('CBA') #等价于 re.search('A ...

  8. 迷你版AOP框架

    http://www.cnblogs.com/artech/archive/2008/11/27/1342309.html

  9. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_03-用户认证-认证服务查询数据库-查询用户接口-接口定义

    1.2.4 查询用户接口 完成用户中心根据账号查询用户信息接口功能. 在ucenter这个服务里面定义查询用户信息的接口 这个接口在auth的服务的loadUserByUserName这个方法里面被调 ...

  10. RVCT编译错误 Cannot obtain license for Compiler

    找不到文件 找不到文件 Error: C9932E: Cannot obtain license for Compiler (feature compiler) with license versio ...