python reportlab 生成多页pdf

多页

from reportlab.pdfgen import canvas
from reportlab.platypus import (SimpleDocTemplate, Paragraph, PageBreak, Image, Spacer, Table, TableStyle)
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.pagesizes import LETTER, inch
from reportlab.graphics.shapes import Line, LineShape, Drawing
from reportlab.lib.colors import Colorclass FooterCanvas(canvas.Canvas):def __init__(self, *args, **kwargs):canvas.Canvas.__init__(self, *args, **kwargs)self.pages = []self.width, self.height = LETTERdef showPage(self):self.pages.append(dict(self.__dict__))self._startPage()def save(self):page_count = len(self.pages)for page in self.pages:self.__dict__.update(page)if (self._pageNumber > 1):self.draw_canvas(page_count)canvas.Canvas.showPage(self)canvas.Canvas.save(self)def draw_canvas(self, page_count):page = "Page %s of %s" % (self._pageNumber, page_count)x = 128self.saveState()self.setStrokeColorRGB(0, 0, 0)self.setLineWidth(0.5)self.drawImage("python_logo.png", self.width - inch * 8 - 5, self.height - 50, width=100, height=20,preserveAspectRatio=True)self.drawImage("python_logo.png", self.width - inch * 2, self.height - 50, width=100, height=30,preserveAspectRatio=True, mask='auto')self.line(30, 740, LETTER[0] - 50, 740)self.line(66, 78, LETTER[0] - 66, 78)self.setFont('Times-Roman', 10)self.drawString(LETTER[0] - x, 65, page)self.restoreState()class PDFPSReporte:def __init__(self, path):self.path = pathself.styleSheet = getSampleStyleSheet()self.elements = []# colors - Azul turkeza 367AB3self.colorOhkaGreen0 = Color((45.0 / 255), (166.0 / 255), (153.0 / 255), 1)self.colorOhkaGreen1 = Color((182.0 / 255), (227.0 / 255), (166.0 / 255), 1)self.colorOhkaGreen2 = Color((140.0 / 255), (222.0 / 255), (192.0 / 255), 1)# self.colorOhkaGreen2 = Color((140.0/255), (222.0/255), (192.0/255), 1)self.colorOhkaBlue0 = Color((54.0 / 255), (122.0 / 255), (179.0 / 255), 1)self.colorOhkaBlue1 = Color((122.0 / 255), (180.0 / 255), (225.0 / 255), 1)self.colorOhkaGreenLineas = Color((50.0 / 255), (140.0 / 255), (140.0 / 255), 1)self.firstPage()self.nextPagesHeader(True)self.remoteSessionTableMaker()self.nextPagesHeader(False)self.inSiteSessionTableMaker()self.nextPagesHeader(False)self.extraActivitiesTableMaker()self.nextPagesHeader(False)self.summaryTableMaker()# Buildself.doc = SimpleDocTemplate(path, pagesize=LETTER)self.doc.multiBuild(self.elements, canvasmaker=FooterCanvas)def firstPage(self):img = Image('python_logo.png', kind='proportional')img.drawHeight = 0.5 * inchimg.drawWidth = 2.4 * inchimg.hAlign = 'LEFT'self.elements.append(img)spacer = Spacer(30, 100)self.elements.append(spacer)img = Image('python_logo.png')img.drawHeight = 2.5 * inchimg.drawWidth = 5.5 * inchself.elements.append(img)spacer = Spacer(10, 250)self.elements.append(spacer)psDetalle = ParagraphStyle('Resumen', fontSize=9, leading=14, justifyBreaks=1, alignment=TA_LEFT,justifyLastLine=1)text = """REPORTE DE SERVICIOS PROFESIONALES<br/>Empresa: Nombre del Cliente<br/>Fecha de Inicio: 23-Oct-2019<br/>Fecha de actualización: 01-Abril-2020<br/>"""paragraphReportSummary = Paragraph(text, psDetalle)self.elements.append(paragraphReportSummary)self.elements.append(PageBreak())def nextPagesHeader(self, isSecondPage):if isSecondPage:psHeaderText = ParagraphStyle('Hed0', fontSize=16, alignment=TA_LEFT, borderWidth=3,textColor=self.colorOhkaGreen0)text = 'REPORTE DE SESIONES'paragraphReportHeader = Paragraph(text, psHeaderText)self.elements.append(paragraphReportHeader)spacer = Spacer(10, 10)self.elements.append(spacer)d = Drawing(500, 1)line = Line(-15, 0, 483, 0)line.strokeColor = self.colorOhkaGreenLineasline.strokeWidth = 2d.add(line)self.elements.append(d)spacer = Spacer(10, 1)self.elements.append(spacer)d = Drawing(500, 1)line = Line(-15, 0, 483, 0)line.strokeColor = self.colorOhkaGreenLineasline.strokeWidth = 0.5d.add(line)self.elements.append(d)spacer = Spacer(10, 22)self.elements.append(spacer)def remoteSessionTableMaker(self):psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3,textColor=self.colorOhkaBlue0)text = 'SESIONES REMOTAS'paragraphReportHeader = Paragraph(text, psHeaderText)self.elements.append(paragraphReportHeader)spacer = Spacer(10, 22)self.elements.append(spacer)"""Create the line items"""d = []textData = ["No.", "Fecha", "Hora Inicio", "Hora Fin", "Tiempo Total"]fontSize = 8centered = ParagraphStyle(name="centered", alignment=TA_CENTER)for text in textData:ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)titlesTable = Paragraph(ptext, centered)d.append(titlesTable)data = [d]lineNum = 1formattedLineData = []alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),ParagraphStyle(name="02", alignment=TA_LEFT),ParagraphStyle(name="03", alignment=TA_CENTER),ParagraphStyle(name="04", alignment=TA_CENTER),ParagraphStyle(name="05", alignment=TA_CENTER)]for row in range(10):lineData = [str(lineNum), "Miércoles, 11 de diciembre de 2019","17:30", "19:24", "1:54"]# data.append(lineData)columnNumber = 0for item in lineData:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[columnNumber])formattedLineData.append(p)columnNumber = columnNumber + 1data.append(formattedLineData)formattedLineData = []# Row for totaltotalRow = ["Total de Horas", "", "", "", "30:15"]for item in totalRow:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[1])formattedLineData.append(p)data.append(formattedLineData)# print(data)table = Table(data, colWidths=[50, 200, 80, 80, 80])tStyle = TableStyle([  # ('GRID',(0, 0), (-1, -1), 0.5, grey),('ALIGN', (0, 0), (0, -1), 'LEFT'),# ('VALIGN', (0, 0), (-1, -1), 'TOP'),("ALIGN", (1, 0), (1, -1), 'RIGHT'),('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),('BACKGROUND', (0, 0), (-1, 0), self.colorOhkaGreenLineas),('BACKGROUND', (0, -1), (-1, -1), self.colorOhkaBlue1),('SPAN', (0, -1), (-2, -1))])table.setStyle(tStyle)self.elements.append(table)def inSiteSessionTableMaker(self):self.elements.append(PageBreak())psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3,textColor=self.colorOhkaBlue0)text = 'SESIONES EN SITIO'paragraphReportHeader = Paragraph(text, psHeaderText)self.elements.append(paragraphReportHeader)spacer = Spacer(10, 22)self.elements.append(spacer)"""Create the line items"""d = []textData = ["No.", "Fecha", "Hora Inicio", "Hora Fin", "Tiempo Total"]fontSize = 8centered = ParagraphStyle(name="centered", alignment=TA_CENTER)for text in textData:ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)titlesTable = Paragraph(ptext, centered)d.append(titlesTable)data = [d]lineNum = 1formattedLineData = []alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),ParagraphStyle(name="02", alignment=TA_LEFT),ParagraphStyle(name="03", alignment=TA_CENTER),ParagraphStyle(name="04", alignment=TA_CENTER),ParagraphStyle(name="05", alignment=TA_CENTER)]for row in range(10):lineData = [str(lineNum), "Miércoles, 11 de diciembre de 2019","17:30", "19:24", "1:54"]# data.append(lineData)columnNumber = 0for item in lineData:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[columnNumber])formattedLineData.append(p)columnNumber = columnNumber + 1data.append(formattedLineData)formattedLineData = []# Row for totaltotalRow = ["Total de Horas", "", "", "", "30:15"]for item in totalRow:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[1])formattedLineData.append(p)data.append(formattedLineData)# print(data)table = Table(data, colWidths=[50, 200, 80, 80, 80])tStyle = TableStyle([  # ('GRID',(0, 0), (-1, -1), 0.5, grey),('ALIGN', (0, 0), (0, -1), 'LEFT'),# ('VALIGN', (0, 0), (-1, -1), 'TOP'),("ALIGN", (1, 0), (1, -1), 'RIGHT'),('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),('BACKGROUND', (0, 0), (-1, 0), self.colorOhkaGreenLineas),('BACKGROUND', (0, -1), (-1, -1), self.colorOhkaBlue1),('SPAN', (0, -1), (-2, -1))])table.setStyle(tStyle)self.elements.append(table)def extraActivitiesTableMaker(self):self.elements.append(PageBreak())psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3,textColor=self.colorOhkaBlue0)text = 'OTRAS ACTIVIDADES Y DOCUMENTACIÓN'paragraphReportHeader = Paragraph(text, psHeaderText)self.elements.append(paragraphReportHeader)spacer = Spacer(10, 22)self.elements.append(spacer)"""Create the line items"""d = []textData = ["No.", "Fecha", "Hora Inicio", "Hora Fin", "Tiempo Total"]fontSize = 8centered = ParagraphStyle(name="centered", alignment=TA_CENTER)for text in textData:ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)titlesTable = Paragraph(ptext, centered)d.append(titlesTable)data = [d]lineNum = 1formattedLineData = []alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),ParagraphStyle(name="02", alignment=TA_LEFT),ParagraphStyle(name="03", alignment=TA_CENTER),ParagraphStyle(name="04", alignment=TA_CENTER),ParagraphStyle(name="05", alignment=TA_CENTER)]for row in range(10):lineData = [str(lineNum), "Miércoles, 11 de diciembre de 2019","17:30", "19:24", "1:54"]# data.append(lineData)columnNumber = 0for item in lineData:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[columnNumber])formattedLineData.append(p)columnNumber = columnNumber + 1data.append(formattedLineData)formattedLineData = []# Row for totaltotalRow = ["Total de Horas", "", "", "", "30:15"]for item in totalRow:ptext = "<font size='%s'>%s</font>" % (fontSize - 1, item)p = Paragraph(ptext, alignStyle[1])formattedLineData.append(p)data.append(formattedLineData)# print(data)table = Table(data, colWidths=[50, 200, 80, 80, 80])tStyle = TableStyle([  # ('GRID',(0, 0), (-1, -1), 0.5, grey),('ALIGN', (0, 0), (0, -1), 'LEFT'),# ('VALIGN', (0, 0), (-1, -1), 'TOP'),("ALIGN", (1, 0), (1, -1), 'RIGHT'),('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),('BACKGROUND', (0, 0), (-1, 0), self.colorOhkaGreenLineas),('BACKGROUND', (0, -1), (-1, -1), self.colorOhkaBlue1),('SPAN', (0, -1), (-2, -1))])table.setStyle(tStyle)self.elements.append(table)def summaryTableMaker(self):self.elements.append(PageBreak())psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3,textColor=self.colorOhkaBlue0)text = 'REGISTRO TOTAL DE HORAS'paragraphReportHeader = Paragraph(text, psHeaderText)self.elements.append(paragraphReportHeader)spacer = Spacer(10, 22)self.elements.append(spacer)"""Create the line items"""tStyle = TableStyle([('ALIGN', (0, 0), (0, -1), 'LEFT'),# ('VALIGN', (0, 0), (-1, -1), 'TOP'),("ALIGN", (1, 0), (1, -1), 'RIGHT'),('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),('BACKGROUND', (-2, -1), (-1, -1), self.colorOhkaGreen2)])fontSize = 8lineData = [["Sesiones remotas", "30:15"],["Sesiones en sitio", "00:00"],["Otras actividades", "00:00"],["Total de horas consumidas", "30:15"]]# for row in lineData:#     for item in row:#         ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)#         p = Paragraph(ptext, centered)#         formattedLineData.append(p)#     data.append(formattedLineData)#     formattedLineData = []table = Table(lineData, colWidths=[400, 100])table.setStyle(tStyle)self.elements.append(table)# Total de horas contradas vs horas consumidasdata = []formattedLineData = []lineData = [["Total de horas contratadas", "120:00"],["Horas restantes por consumir", "00:00"]]# for row in lineData:#     for item in row:#         ptext = "<b>{}</b>".format(item)#         p = Paragraph(ptext, self.styleSheet["BodyText"])#         formattedLineData.append(p)#     data.append(formattedLineData)#     formattedLineData = []table = Table(lineData, colWidths=[400, 100])tStyle = TableStyle([('ALIGN', (0, 0), (0, -1), 'LEFT'),("ALIGN", (1, 0), (1, -1), 'RIGHT'),('BACKGROUND', (0, 0), (1, 0), self.colorOhkaBlue1),('BACKGROUND', (0, 1), (1, 1), self.colorOhkaGreen1),])table.setStyle(tStyle)spacer = Spacer(10, 50)self.elements.append(spacer)self.elements.append(table)if __name__ == '__main__':report = PDFPSReporte('psreport.pdf')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/146560.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【CVPR 2023】DSVT: Dynamic Sparse Voxel Transformer with Rotated Sets

文章目录 开场白效果意图 重点VoxelNet: End-to-End Learning for Point Cloud Based 3D Object DetectionX-Axis DSVT LayerY-Axis DSVT Layer Dynamic Sparse Window AttentionDynamic set partitionRotated set attention for intra-window feature propagation.Hybrid wind…

优维低代码实践:应用级配置

优维低代码技术专栏&#xff0c;是一个全新的、技术为主的专栏&#xff0c;由优维技术委员会成员执笔&#xff0c;基于优维7年低代码技术研发及运维成果&#xff0c;主要介绍低代码相关的技术原理及架构逻辑&#xff0c;目的是给广大运维人提供一个技术交流与学习的平台。 优维…

[NOIP2012 提高组] 开车旅行

[NOIP2012 提高组] 开车旅行 题目描述 小 A \text{A} A 和小 B \text{B} B 决定利用假期外出旅行&#xff0c;他们将想去的城市从 $1 $ 到 n n n 编号&#xff0c;且编号较小的城市在编号较大的城市的西边&#xff0c;已知各个城市的海拔高度互不相同&#xff0c;记城市 …

亚信科技AntDB数据库 高并发、低延迟、无死锁,深入了解AntDB-M元数据锁的实现

AntDB-M在架构上分为两层&#xff0c;服务层和存储引擎层。元数据的并发管理集中在服务层&#xff0c;数据的存储访问在存储引擎层。为了保证DDL操作与DML操作之间的一致性&#xff0c;引入了元数据锁&#xff08;MDL&#xff09;。 AntDB-M提供了丰富的元数据锁功能&#xff0…

Koa处理请求数据

在开发中&#xff0c;后端接收到请求参数后&#xff0c;需要解析参数。请求分为很多种类型&#xff0c;比如常见的get和post。 请求参数 Koa本身可以解析get请求参数&#xff0c;不能解析post请求参数。例如&#xff1a; router.get(/api/get/userInfo, async (context) >…

新手--安装好Quartus II13.0(带modelsim集成包)并用Quartus II搭建一个工程

前言 今天是国庆节&#xff0c;我们正式来学习Quartus II13.0软件的安装与使用。学习verilog与学习C语言都是学习一门语言&#xff0c;那么学习一门语言&#xff0c;光看理论不敲代码绝对是学习不好的。要用verilog语言敲代码&#xff0c;就要像C语言那样搭建起语言的编译环境&…

C语言 Cortex-A7核 IIC实验

iic.h #ifndef __IIC_H__ #define __IIC_H__ #include "stm32mp1xx_gpio.h" #include "stm32mp1xx_rcc.h" /* 通过程序模拟实现I2C总线的时序和协议* GPIOF ---> AHB4* I2C1_SCL ---> PF14* I2C1_SDA ---> PF15** */#define SET_SDA_OUT do{…

B. Comparison String

题目&#xff1a; 样例&#xff1a; 输入 4 4 <<>> 4 >><< 5 >>>>> 7 <><><><输出 3 3 6 2 思路&#xff1a; 由题意&#xff0c;条件是 又因为要使用尽可能少的数字&#xff0c;这是一道贪心题&#xff0c;所以…

Linux CentOS7 vim临时文件

在vim中&#xff0c;由于断网、停电、故意退出、不小心关闭终端等多种原因&#xff0c;正在编辑的文件没有保存&#xff0c;系统将会为文件保存一个交换文件&#xff0c;或称临时文件&#xff0c;或备份文件。 如果因某种原因产生了交换文件&#xff0c;每次打开文件时&#x…

详解分布式搜索技术之elasticsearch

目录 一、初识elasticsearch 1.1什么是elasticsearch 1.2elasticsearch的发展 1.3为什么学习elasticsearch? 1.4正向索引和倒排索引 1.4.1传统数据库采用正向索引 1.4.2elasticsearch采用倒排索引 1.4.3posting list ​1.4.4总结 1.5 es的一些概念 1.5.1文档和字段 …

鞋类 整鞋试验方法 剥离强度

声明 本文是学习GB-T 3903.3-2011 鞋类 整鞋试验方法 剥离强度. 而整理的学习笔记,分享出来希望更多人受益,如果存在侵权请及时联系我们 1 范围 GB/T 3903 的本部分规定了整鞋鞋底与鞋帮或外底与外中底之间剥离强度的试验方法。 本部分适用于采用模压、硫化、注塑、灌注、胶…

C进阶--字符函数和字符串函数介绍

✨ 更多细节参考 cplusplus.com/reference/cstring/ 使用方式&#xff1a; ⭕ 求字符串长度 &#x1f58c; strlen 函数原型&#xff1a; size_t strlen ( const char * str ); 作用&#xff1a; 获取字符串长度 ✨补充&#xff1a; ⭐字符串以 \0 作为结束标志&…

5.外部中断

中断初始化配置步骤&#xff1a; IO口初始化配置 开启中断总允许EA 打开某个IO口的中断允许 打开IO口的某一位的中断允许 配置该位的中断触发方式 中断函数&#xff1a; #pragma vector PxINT_VECTOR __interrupt void 函数名(void){}#pragma vector PxINT_VECTOR __int…

喝健康白酒 有益生心健康

中国的制酒史源远流长&#xff0c;酒渗透在中华五千年的文化中。酒与烟不同&#xff0c;烟对人体有百害而无一利&#xff0c;而对于酒&#xff0c;若掌握好饮酒的度&#xff0c;对人体有一定的养生作用&#xff0c;所以我们通常会说“戒烟限酒”。 据一些专家研究&#xff0c;…

云原生Kubernetes:对外服务之 Ingress

目录 一、理论 1.Ingress 2.部署 nginx-ingress-controller(第一种方式) 3.部署 nginx-ingress-controller(第二种方式) 二、实验 1.部署 nginx-ingress-controller(第一种方式) 2.部署 nginx-ingress-controller(第二种方式) 三、问题 1.启动 nginx-ingress-controll…

什么是 MyBatis?与 Hibernate 的区别

引言 在现代的应用程序开发中&#xff0c;与数据库的交互是至关重要的。为了简化数据库访问&#xff0c;许多开发者选择使用ORM&#xff08;对象-关系映射&#xff09;框架。MyBatis和Hibernate都是流行的ORM框架&#xff0c;它们可以帮助开发者更轻松地将Java对象映射到数据库…

Java-API简析_java.util.Objects类(基于 Latest JDK)(浅析源码)

【版权声明】未经博主同意&#xff0c;谢绝转载&#xff01;&#xff08;请尊重原创&#xff0c;博主保留追究权&#xff09; https://blog.csdn.net/m0_69908381/article/details/133463511 出自【进步*于辰的博客】 因为我发现目前&#xff0c;我对Java-API的学习意识比较薄弱…

Polygon Miden交易模型:Actor模式 + ZKP => 并行 + 隐私

1. 引言 前序博客&#xff1a; Polygon Miden&#xff1a;扩展以太坊功能集的ZK-optimized rollupPolygon Miden zkRollup中的UTXO账户混合状态模型 Polygon Miden为&#xff1a; ZK-optimized rollup由客户端生成证明完善Polygon ZK系列解决方案&#xff0c;致力于成为网络…

消息队列-RabbitMQ(二)

接上文《消息队列-RabbitMQ&#xff08;一&#xff09;》 Configuration public class RabbitMqConfig {// 消息的消费方json数据的反序列化Beanpublic RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){Simple…

数据结构-哈希表

系列文章目录 1.集合-Collection-CSDN博客​​​​​​ 2.集合-List集合-CSDN博客 3.集合-ArrayList源码分析(面试)_喜欢吃animal milk的博客-CSDN博客 4.数据结构-哈希表_喜欢吃animal milk的博客-CSDN博客 文章目录 目录 系列文章目录 文章目录 前言 一 . 什么是哈希表&a…