'''
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. 内部类访问局部变量时,为什么需要加final关键字

    是变量的作用域的问题,因为匿名内部类是出现在一个方法的内部的,如果它要访问这个方法的参数或者方法中定义的变量,则这些参数和变量必须被修饰为final.因为虽然匿名内部类在方法的内部,但实际编译的时候, ...

  2. java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the server

    错误信息 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ...

  3. REST和SOAP的区别

    转自:https://www.cnblogs.com/MissQing/p/7240146.html REST似乎在一夜间兴起了,这可能引起一些争议,反对者可以说REST是WEB诞生之始甚而是HTTP ...

  4. react组件中返回并列元素的方法

    我们在写react组件的时候,经常会遇到这种问题,在render中return元素只能有一个顶级元素,比如div,假如写成这样就会报错: render(){ return( <div>12 ...

  5. Web前端笔记整理

    不使用Ajax无刷新提交: header('HTTP/1.1 204 No Content'); var a=document.createElement('img'); a.setAttribute ...

  6. 通过Redis的list来实现 Server - Client 的同步通信

    Redis实现类似同步方法调用的功能(一) 首先声明,这么干纯粹是为了好玩. 通常我们用Redis主要是为了存储一些数据,由于数据在内存里,所以查询更新很快.同时我们也可以利用 Pub/Sub 功能来 ...

  7. 39 Flutter仿京东商城项目 收货地址列表、增加 修改收货地址布局、弹出省市区选择器

    加群452892873 下载对应39课文件,运行方法,建好项目,直接替换lib目录 pubspec.yaml city_pickers: ^ AddressAdd.dart import 'packa ...

  8. 36 Flutter仿京东商城项目 用户登录 退出登录 事件广播更新状态

    Login.dart import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/material.da ...

  9. springboot集成调用Azkaban

    springboot集成调用Azkaban 一. 说明 1.Azkaban是由Linkedin公司推出的一个批量工作流任务调度器,主要用于在一个工作流内以一个特定的顺序运行一组工作和流程,它的配置是通 ...

  10. 123457123456#0#-----com.twoapp.HuaXueGame01--前拼后广--儿童滑雪大冒险游戏jiemei

    com.twoapp.HuaXueGame01--前拼后广--儿童滑雪大冒险游戏jiemei