• 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 Color
    
    
    class FooterCanvas(canvas.Canvas):
    
        def __init__(self, *args, **kwargs):
            canvas.Canvas.__init__(self, *args, **kwargs)
            self.pages = []
            self.width, self.height = LETTER
    
        def 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 = 128
            self.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 = path
            self.styleSheet = getSampleStyleSheet()
            self.elements = []
    
            # colors - Azul turkeza 367AB3
            self.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()
            # Build
            self.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 * inch
            img.drawWidth = 2.4 * inch
            img.hAlign = 'LEFT'
            self.elements.append(img)
    
            spacer = Spacer(30, 100)
            self.elements.append(spacer)
    
            img = Image('python_logo.png')
            img.drawHeight = 2.5 * inch
            img.drawWidth = 5.5 * inch
            self.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
    Empresa: Nombre del Cliente
    Fecha de Inicio: 23-Oct-2019
    Fecha de actualización: 01-Abril-2020
    """
    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.colorOhkaGreenLineas line.strokeWidth = 2 d.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.colorOhkaGreenLineas line.strokeWidth = 0.5 d.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 = 8 centered = ParagraphStyle(name="centered", alignment=TA_CENTER) for text in textData: ptext = "%s" % (fontSize, text) titlesTable = Paragraph(ptext, centered) d.append(titlesTable) data = [d] lineNum = 1 formattedLineData = [] 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 = 0 for item in lineData: ptext = "%s" % (fontSize - 1, item) p = Paragraph(ptext, alignStyle[columnNumber]) formattedLineData.append(p) columnNumber = columnNumber + 1 data.append(formattedLineData) formattedLineData = [] # Row for total totalRow = ["Total de Horas", "", "", "", "30:15"] for item in totalRow: ptext = "%s" % (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 = 8 centered = ParagraphStyle(name="centered", alignment=TA_CENTER) for text in textData: ptext = "%s" % (fontSize, text) titlesTable = Paragraph(ptext, centered) d.append(titlesTable) data = [d] lineNum = 1 formattedLineData = [] 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 = 0 for item in lineData: ptext = "%s" % (fontSize - 1, item) p = Paragraph(ptext, alignStyle[columnNumber]) formattedLineData.append(p) columnNumber = columnNumber + 1 data.append(formattedLineData) formattedLineData = [] # Row for total totalRow = ["Total de Horas", "", "", "", "30:15"] for item in totalRow: ptext = "%s" % (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 = 8 centered = ParagraphStyle(name="centered", alignment=TA_CENTER) for text in textData: ptext = "%s" % (fontSize, text) titlesTable = Paragraph(ptext, centered) d.append(titlesTable) data = [d] lineNum = 1 formattedLineData = [] 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 = 0 for item in lineData: ptext = "%s" % (fontSize - 1, item) p = Paragraph(ptext, alignStyle[columnNumber]) formattedLineData.append(p) columnNumber = columnNumber + 1 data.append(formattedLineData) formattedLineData = [] # Row for total totalRow = ["Total de Horas", "", "", "", "30:15"] for item in totalRow: ptext = "%s" % (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 = 8 lineData = [["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 = "%s" % (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 consumidas data = [] formattedLineData = [] lineData = [["Total de horas contratadas", "120:00"], ["Horas restantes por consumir", "00:00"]] # for row in lineData: # for item in row: # ptext = "{}".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')
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
  • 相关阅读:
    动力总成悬置系统刚度及模态有效质量计算公式推导
    产品经理的AI大模型学习之旅
    PPT效果有限、Echarts技术太高,还是这个工具最快实现报表可视化
    Python下载与安装进阶
    德国跨国汽车巨头大陆集团遭LockBit勒索软件组织攻击
    数据分析的几个角度
    太空射击第16课: 道具(Part 2)
    基于Rook+Ceph的云原生存储架构剖析
    SpringBoot几个常用的注解
    自定义控件——视图的构建过程——视图的绘制方法
  • 原文地址:https://blog.csdn.net/philosophyatmath/article/details/133297323