Advertisement
ALEXANDAR_GEORGIEV

Vertical_text_now

Nov 26th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | Source Code | 0 0
  1. # rotatedtext.py
  2. from reportlab.platypus.flowables import Flowable
  3.  
  4.  
  5. class verticalText(Flowable):
  6.  
  7.     '''Rotates a text in a table cell.'''
  8.  
  9.     def __init__(self, text):
  10.         Flowable.__init__(self)
  11.         self.text = text
  12.  
  13.     def draw(self):
  14.         canvas = self.canv
  15.         canvas.rotate(90)
  16.         fs = canvas._fontsize
  17.         canvas.translate(1, -fs/1.2)  # canvas._leading?
  18.         canvas.drawString(0, 0, self.text)
  19.  
  20.     def wrap(self, aW, aH):
  21.         canv = self.canv
  22.         fn, fs = canv._fontname, canv._fontsize
  23.         return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)
  24.  
  25.  
  26. # vertical_text_table.py
  27. from reportlab.pdfbase import pdfmetrics
  28. from reportlab.pdfbase.ttfonts import TTFont
  29. from reportlab.lib import colors
  30. from reportlab.lib.colors import HexColor
  31. from reportlab.lib.pagesizes import letter
  32. from reportlab.lib.styles import getSampleStyleSheet
  33. from reportlab.lib.units import inch
  34. from reportlab.platypus import (
  35.       BaseDocTemplate, Frame, Paragraph, NextPageTemplate,
  36.       PageBreak, PageTemplate, Image, Table, TableStyle, Spacer)
  37. # from rotatedtext import verticalText
  38.  
  39. document = BaseDocTemplate(
  40.     'Vertical.pdf')
  41.  
  42. Elements = []
  43.  
  44. titleFrame_1 = Frame(
  45.     0.5*inch, 0.75*inch, 7*inch, 9*inch, id='col1', showBoundary=0)
  46. titleTemplate_1 = PageTemplate(
  47.     id='OneCol', frames=titleFrame_1)
  48. document.addPageTemplates([titleTemplate_1])
  49.  
  50. cw = [1.2*inch] + [1*inch]*6
  51. rh = [0.25*inch] + [.6*inch] + [0.25*inch]*7
  52.  
  53. data = [
  54.     ['Some', 'Reporting', '', 'Data', '', 'Here', ''],
  55.     ['', verticalText('Vertical'), verticalText('Text'),
  56.         verticalText('Vertical'), verticalText('Text'),
  57.         verticalText('Vertical'), verticalText('Text')],
  58.     ['Row1', '0', '0', '69', '803', '20751', '11627'],
  59.     ['Row2', '0', '0', '1', '0', '1096', '103'],
  60.     ['Row3', '0', '0', '0', '0', '233', '1'],
  61.     ['Row4', '0', '0', '0', '0', '694', '38'],
  62.     ['Row5', '0', '0', '23', '2', '1319', '2'],
  63.     ['Row6', '0', '0', '0', '0', '0', '0'],
  64.     ['TOTAL', '0', '0', '93', '805', '24093', '11771'],
  65.     ]
  66.  
  67. ts = [
  68.     ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
  69.     ('SPAN', (1, 0), (2, 0)),
  70.     ('SPAN', (3, 0), (4, 0)),
  71.     ('SPAN', (5, 0), (6, 0)),
  72.     ('SPAN', (0, 0), (0, 1)),
  73.     ('ALIGN', (0, 0), (-1, 1), 'CENTER'),
  74.     ('ALIGN', (0, 2), (-1, -1), 'RIGHT'),
  75.     ('VALIGN', (0, 0), (-1, -2), 'MIDDLE'),
  76.     ('FONT', (0, 0), (-1, 1), 'Helvetica-Bold', 7, 7),
  77.     ('FONT', (0, 2), (0, -2), 'Helvetica-Bold', 7, 7),
  78.     ('FONT', (1, 2), (-1, -2), 'Helvetica', 7, 7),
  79.     ('FONT', (0, -1), (-1, -1), 'Helvetica-Bold', 8, 8),
  80.     ('TEXTCOLOR', (0, -1), (-1, -1), colors.white),
  81.     ('BACKGROUND', (0, -1), (-1, -1), colors.black),
  82. ]
  83.  
  84. t = Table(
  85.     data, style=ts,
  86.     colWidths=cw, rowHeights=rh)
  87.  
  88. Elements.append(t)
  89. document.build(Elements)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement