Python & ReportLab: wrong column width and alignment in PDF table

I am using Python 2.7 (iOS Pythonista application) and the reportlab 2.7 module to create a PDF file with a table. Everything is working fine. RepotLab automatically formats the width of the columns. But in two cases, I can’t understand why reportlab formats the output the way it does, and how I can get the desired format.

Case 1: All I Want ...

+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Day | Date     | Time          | Duration | Notes                                                                                                         |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.25     | Here are some notes.                                                                                          |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Wed | 02.04.14 | 18:00 - 20:15 | 2.25     | Sometime these notes are a little longer text so there must be a line break to let the whole note be visible! |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Thu | 02.04.14 | 14:00 - 17:15 | 3.25     | And sometimes these notes are only a few words.                                                               |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+

Case 2: I am trying to align floating point

Via

TableStyle([('ALIGN', (3,1), (3,-1), 'DECIMAL'),])

. , "" () , "" ( "" ). , , .

+-----+----------+---------------+-----------+-------------------------------------------------+
| Day | Date     | Time          | Duration  | Notes                                           |
+-----+----------+---------------+-----------+-------------------------------------------------+
| Tue | 01.04.14 | 14:00 - 17:15 |        3.2|5Here are some notes.                            |
+-----+----------+---------------+-----------+-------------------------------------------------+
| Wed | 02.04.14 | 14:00 - 17:15 |        3.2|5And sometimes these notes are only a few words. |
+-----+----------+---------------+-----------+-------------------------------------------------+

TableStyle([('RIGHTPADDING', (3,1), (3,-1), 18),])

, , !

3: .

, , . , .

(DIN A4 ). , . "", "", "" "" , , "" , . , , , ;-).

+-----+----------+---------------+----------+----------------------+
| Day | Date     | Time          | Duration | Notes                |
+-----+----------+---------------+----------+----------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.25     | Here are some notes. |
+-----+----------+---------------+----------+----------------------+
| Wed | 02.04.14 | 18:00 - 20:15 | 2.25     | Sometime these       |
|     |          |               |          | notes are a          |
|     |          |               |          | little longer        |
|     |          |               |          | text so there        |
|     |          |               |          | must be a line       |
|     |          |               |          | break to let         |
|     |          |               |          | the whole note       |
|     |          |               |          | be visible!          |
+-----+----------+---------------+----------+----------------------+
| Thu | 02.04.14 | 14:00 - 17:15 | 3.25     | And sometimes        |
|     |          |               |          | these notes are      |
|     |          |               |          | only a few words.    |
+-----+----------+---------------+----------+----------------------+

, :

import pipista
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle

styleSheet = getSampleStyleSheet()

def makeReportData(n):
  monthreport = []
  monthreport += ('Day', '', 'Time', 'Duration', 'Notes'),
  monthreport += ('Tue', '01.04.14', '14:00 - 17:15', '3.25', n[0]),
  monthreport += ('Wed', '02.04.14', '14:00 - 17:15', '3.25', n[1]),
  monthreport += ('Thu', '03.04.14', '14:00 - 17:15', '3.25', n[2]),
  return monthreport

notes = ['Here are some notes.',
         'Sometime these notes are a little longer text so there must be a line break to let the whole note be visible!',
         'And sometimes these notes are only a few words.']

paranote = []
for note in notes:
  paranote += (Paragraph(note, styleSheet["BodyText"]),)

tstyle = [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
          ('BOX', (0,0), (-1,-1), 0.25, colors.black),]

case1 = []
t = Table(makeReportData(notes))
t.setStyle(TableStyle(tstyle))
case1.append(t)
doc = SimpleDocTemplate('Table normal.pdf', pagesize = A4)
doc.build(case1)

case2 = []
tstyledez = tstyle + [('ALIGN', (3,1), (3,-1), 'DECIMAL'),]
t.setStyle(TableStyle(tstyledez))
case2.append(t)
doc = SimpleDocTemplate('Table align decimal.pdf', pagesize = A4)
doc.build(case2)

case3 = []
t = Table(makeReportData(paranote))
tstylepara = tstyle + [('VALIGN',(0,1),(3,-1),'TOP'),]
t.setStyle(TableStyle(tstylepara))
case3.append(t)
doc = SimpleDocTemplate('Table Paragraph.pdf', pagesize = A4)
doc.build(case3)

, - .

+4
1

, RTM ! ReportLab:

Flowable Flowables, , .

:

from reportlab.lib.units import mm

t = Table(makeReportData(paranote), colWidths=(None, None, None, None, 100*mm))

, DECIMAL ( 2) , .

+6

Source: https://habr.com/ru/post/1539790/


All Articles