Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # vertical_text_table.py
- from reportlab.pdfbase import pdfmetrics
- from reportlab.pdfbase.ttfonts import TTFont
- from reportlab.lib import colors
- from reportlab import *
- from reportlab.lib.colors import HexColor
- from reportlab.lib.pagesizes import letter
- from reportlab.lib.styles import getSampleStyleSheet
- from reportlab.lib.units import inch
- from reportlab.platypus import (BaseDocTemplate, Frame, Paragraph, NextPageTemplate,
- PageBreak, PageTemplate, Image, Table, TableStyle, Spacer)
- #from rotatedtext import verticalText
- from reportlab.platypus.flowables import Flowable
- from reportlab.pdfgen import canvas
- import textwrap
- pdfmetrics.registerFont(TTFont('Times','Times.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbd','Timesbd.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesi','Timesi.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbi','Timesbi.ttf', 'UTF-8'))
- class verticalText(Flowable): # Rotates a text in a table cell.
- def __init__(self, text):
- Flowable.__init__(self)
- self.text = text
- def draw(self):
- canvas = self.canv
- canvas.rotate(90)
- fs = canvas._fontsize
- # Измества канваса малко надясно
- canvas.translate(1, -fs/1.2) # canvas._leading?
- canvas.drawString(0, 0, self.text)
- def wrap(self, aW, aH):
- canv = self.canv
- fn, fs = canv._fontname, canv._fontsize
- return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)
- class verticalText1(Flowable): # Rotates a text in a table cell.
- def __init__(self, text):
- Flowable.__init__(self)
- self.text = text
- def draw(self):
- canvas = self.canv
- canvas.rotate(90)
- fs = canvas._fontsize
- # Измества канваса малко надясно
- canvas.translate(1, -fs/1.2) # canvas._leading?
- #canvas.drawString(0, 0, self.text)
- draw_wrapped_line(canvas, self.text, 16, 26, 3, 7)
- def wrap(self, aW, aH):
- canv = self.canv
- fn, fs = canv._fontname, canv._fontsize
- return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)
- def draw_wrapped_line(canvas, text, length, x_pos, y_pos, y_offset):
- """
- :param canvas: reportlab canvas
- :param text: the raw text to wrap
- :param length: the max number of characters per line
- :param x_pos: starting x position
- :param y_pos: starting y position
- :param y_offset: the amount of space to leave between wrapped lines
- """
- if len(text) > length:
- wraps = textwrap.wrap(text, length)
- for x in range(len(wraps)):
- canvas.drawCentredString(x_pos, y_pos, wraps[x])
- y_pos -= y_offset
- y_pos += y_offset # add back offset after last wrapped line
- else:
- canvas.drawCentredString(x_pos, y_pos, text)
- return y_pos
- document = BaseDocTemplate('Vertical_1.pdf')
- Elements = []
- titleFrame_1 = Frame(
- 0.5*inch, 0.75*inch, 7*inch, 9*inch, id='col1', showBoundary=0)
- titleTemplate_1 = PageTemplate(
- id='OneCol', frames=titleFrame_1)
- document.addPageTemplates([titleTemplate_1])
- cw = [1.2*inch] + [1*inch]*6
- rh = [0.25*inch] + [.6*inch] + [0.25*inch]*7
- data = [
- ['ПОКАЗАТЕЛИ', 'Отчетна стойност на\n нетекущите активи', '', '', '', '', '', ''],
- ['', verticalText1('В началото на периода'), verticalText1('На постъпилите през периода'),
- verticalText1('На излезлите през периода'), verticalText1('В края на периода (1+2-3)'),
- verticalText1('Последваща оцен-ка в увеличение'), verticalText1('Последваща оцен-ка в намаление'),
- verticalText1('Преоценена стой-ност (4+5-6)')],
- ['a', '1','2','3','4','5','6','7'],
- ['I. Нематериални активи', '', '','', '', '', '', ''],
- [' 1. Продукти от развойна дейност', '0', '0', '', '233', '1', '3', '555'],
- [' 2. Концесии, патенти, търговски\nмарки, програмни продукти и\nдруги подобни права и активи', '0', '0', '0', '0', '694', '38', '4'],
- [' 3. Търговска репутация', '0', '0', '23', '2', '1319', '2', '5'],
- [' 4. Предоставени аванси и немате-\nриални активи в процес\nна изграждане', '0', '0','0', '0', '0', '0', '6'],
- [' в т.ч.: предоставени аванси', '0', '0', '93', '805', '24093', '11771', '7'],
- ['Общо за група I.', '0', '0', '93', '805', '24093', '11771', '7'],
- ]
- ts = [
- ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
- #('GRID', (0, 1), (0, 1), 0.5, colors.black),
- #('GRID', (3, 1), (3, 1), 0.5, colors.black),
- ('SPAN', (1, 0), (-1, 0)), # Отчтна стойност на нетекущите активи
- ('SPAN', (0, 0), (0, 1)), # Показатели
- #('GRID', (0, 2), (-1, -1), 0.5, colors.black),
- ('ALIGN', (0, 0), (-1, 0), 'CENTER'), # Център по Х -> ПОКАЗАТЕЛИ
- ('VALIGN', (0, 0), (0, 0), 'MIDDLE'), # Цебтър по Y -> ПОКАЗАТЕЛИ
- ('FONT', (0, 0), (0, 0), 'Timesbd', 7), # -> ПОКАЗАТЕЛИ
- ('FONT', (1, 0), (-1, 0), 'Timesbd', 6), # -> ПОКАЗАТЕЛИ
- ('FONT', (1, 1), (-1, 1), 'Times', 7, 6), # -> Vertical text
- ('BACKGROUND', (0, 0), (-1, 2), colors.lightgreen), # lightcoral, lightgreen, lightsalmon, lightseagreen, lightsteelblue
- ('ALIGN', (1, 1), (-1, 1), 'LEFT'), # Център по Х -> Vertical text
- ('VALIGN', (1, 1), (-1, 1), 'BOTTOM'), # Цебтър по Y -> Vertical text
- ('ALIGN', (0, 2), (-1, 2), 'CENTER'), # a, 1, 2, 3 ....7
- ('VALIGN', (0, 2), (-1, 2), 'MIDDLE'), # a, 1, 2, 3 ....7
- ('FONT', (0, 2), (-1, 3), 'Timesbd', 6), # a, 1, 2, 3 ....7
- ('SPAN', (0, 3), (-1, 3)), # I. Нематериални активи
- ('BACKGROUND', (0, 3), (-1, 3), colors.lightgoldenrodyellow), # I. Нематериални активи
- ('ALIGN', (0, 3), (0, -1), 'LEFT'),
- ('VALIGN', (0, 3), (0, -1), 'MIDDLE'),
- # DATA NUMBERS
- ('ALIGN', (1, 3), (-1, -1), 'RIGHT'),
- ('VALIGN', (1, 3), (-1, -1), 'MIDDLE'),
- ('FONT', (1, 4), (-1, -1), 'Times', 7),
- #('LEADING', (1, 3), (-1, -1), 1),
- #('HALIGN', (1, 3), (-1, -1), 0),
- #('LEFTPADDING', (1, 3), (-1, -1), 0),
- ('RIGHTPADDING', (1, 3), (-1, -1), 2),
- ('BOTTOMPADDING', (0, 9), (-1, 9), 6),
- ('TOPPADDING', (0, 3), (0, -1), 0),
- ('FONT', (0, 4), (0, 8), 'Times', 7, 5),
- ('FONT', (0, 9), (-1, 9), 'Timesbd', 7, 5),
- ('BACKGROUND', (0, 9), (-1, 9), colors.lightgoldenrodyellow),
- ]
- t = Table(
- data, style=ts,
- colWidths=[120, 20, 20, 20, 20, 20, 20, 20], rowHeights=[20, 70, 10, 10, 10, 20, 10, 20, 10, 10])
- #colWidths=cw, rowHeights=rh)
- Elements.append(t)
- document.build(Elements)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement