2016-09-21 38 views
1

Ich habe eine Table mit 2-Zellen, innerhalb eines jeden gibt es ein ParagraphÜberlauf auf einem Absatz in Reportlab versteckt

from reportlab.platypus import Paragraph, Table, TableStyle 
from reportlab.lib.styles import ParagraphStyle 
from reportlab.lib.units import cm 

table_style_footer = TableStyle(
      [ 
       ('LEFTPADDING', (0, 0), (-1, -1), 0), 
       ('RIGHTPADDING', (0, 0), (-1, -1), 0), 
       ('TOPPADDING', (0, 0), (-1, -1), 0), 
       ('BOTTOMPADDING', (0, 0), (-1, -1), 0), 
       ('BOX', (0, 0), (-1, -1), 1, (0, 0, 0)), 
       ('VALIGN', (0, 0), (-1, -1), 'TOP'), 
      ] 
     ) 

style_p_footer = ParagraphStyle('Normal') 
style_p_footer.fontName = 'Arial' 
style_p_footer.fontSize = 8 
style_p_footer.leading = 10 

Table([ 
     [ 
     Paragraph('Send To:', style_p_footer), 
     Paragraph('Here should be a variable with long content', style_p_footer) 
     ] 
     ], 
     [1.7 * cm, 4.8 * cm], 
     style=table_style_footer 
    ) 

ich den Überlauf Inhalt des Absatzes verstecken muß, aber der Absatz anstatt den Überlauf von verstecken Inhalt macht eine Bruchlinie.

Antwort

2

Reportlab scheint keine native Unterstützung für das Verbergen von Überlauf zu haben, aber wir können es erreichen, indem wir die breakLines Funktion von Paragraph verwenden. Die breakLines Funktionen gibt ein Objekt zurück, das alle Zeilen des Absatzes mit einer bestimmten Breite enthält. Daher können wir es auch verwenden, um die erste Zeile zu finden und alles andere zu verwerfen.

Grundsätzlich müssen wir folgendes tun:

  1. erstellen Dummy Absatz
  2. Fetch die Linien des Dummy
  3. erstellen die tatsächliche Absatz auf der ersten Zeile des Dummy
basierend

dies wie folgt aussieht in Code tun:

# Create a dummy paragraph to see how it would split 
long_string = 'Here should be a variable with long content'*10 
long_paragraph = Paragraph(long_string, style_p_footer) 

# Needed because of a bug in breakLines (value doesn't matter) 
long_paragraph.width = 4.8 * cm 

# Fetch the lines of the paragraph for the given width 
para_fragment = long_paragraph.breakLines(width=4.8 * cm) 

# There are 2 kinds of returns so 2 ways to grab the first line 
if para_fragment.kind == 0: 
    shorted_text = " ".join(para_fragment.lines[0][1]) 
else: 
    shorted_text = " ".join([w.text for w in para_fragment.lines[0].words]) 

# To make it pretty add ... when we break of the sentence 
if len(para_fragment.lines) > 1: 
    shorted_text += "..." 

# Create the actual paragraph 
shorted_paragraph = Paragraph(shorted_text, style_p_footer) 
+0

Funktioniert perfekt. Ich habe eine Frage, wie kann ich 'para_fragment.kind' von" 0 "unterscheiden? @ B8vrede –

+0

Hinzufügen eines Inline-Seitenumbruch wird dazu führen, dass es Art 1 :) – B8vrede

+0

"Hinzufügen eines Inline-Seitenumbruch" Like \ n? : D –

Verwandte Themen