'''
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. python生成二维码(简易)

    首先要的配置: pillow image qrcode zxing 然后直接上代码: import PIL import qrcode # 实例化二维码生成类 qr = qrcode.QRCode( ...

  2. C语言应用--数据类型定制一结构体数组

    结构体定义成功后,其实和c语言内部的类型区别也不大了,自然可以用来定义结构体类型的数组了.我们根据结构体定义的方式不同,分别用多种方式定义结构体数组:

  3. yum安装nginx添加upstream_check_module模块

    下载模块 upstream_check_module 查看yum安装nginx版本信息 # nginx -V nginx version: nginx/1.17.0 built by gcc 4.8. ...

  4. mkfs格式化分区(为分区写入文件系统)

    mkfs 命令非常简单易用,不过是不能调整分区的默认参数的(比如块大小是 4096 Bytes),这些默认参数除非特殊清况,否则不需要调整.如果想要调整,就需要使用 mke2fs 命令重新格式化.命令 ...

  5. 为什么要装Tomcat?

    来说一说C/S架构和B/S架构   先来说说为什么C/S框架不用对tomcat之类的部署?   其中主要的原因在与这种结构本身就是有服务器来提供服务的,客户端来使用服务.   再者说为什么B/S架构要 ...

  6. LinkedHashSet

    特点: 有序 , 唯一 底层的数据结构为: 链表和哈希表 , 链表保证有序 , 哈希表保证唯一 import java.util.LinkedHashSet; public class Demo2_L ...

  7. osg::Camera 参数修改

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...

  8. EasyUI实现图片的上传后与其他文本框的提交以及DataGrid中图片的展示

    图片即文件,在jsp中文件上传很简单,一个type为file的input,一个form指定enctype为multipart/form-data,通过post提交到后台利用apache的commons ...

  9. 123457123457#0#-----com.yimeng.wangZheChengYu01--前拼后广--成语头脑王者

    com.yimeng.wangZheChengYu01--前拼后广--成语头脑王者

  10. 【leetcode_easy】589. N-ary Tree Preorder Traversal

    problem 589. N-ary Tree Preorder Traversal N叉树的前序遍历 首先复习一下树的4种遍历,前序.中序.后序和层序.其中,前序.中序和后序是根据根节点的顺序进行区 ...