2016-01-20 155 views
0

我在python 2.7中使用reportlab 3.1.44 這是在表中使用段落的代碼。reportlab行間距和表中段落的擬合

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.rl_config import defaultPageSize 
from reportlab.lib.units import inch 
from reportlab.lib.styles import ParagraphStyle 
from reportlab.platypus.tables import Table, TableStyle 
from reportlab.lib import colors 
from reportlab.lib.colors import Color 

styles = getSampleStyleSheet() 


def make_report(): 
    doc = SimpleDocTemplate("hello.pdf") 
    story = [] 
    style = styles["Normal"] 
    ps = ParagraphStyle('title', fontSize=20) 

    p1 = "here is some paragraph to see in large font" 
    data = [] 
    table_row = [Paragraph(p1, ps),\ 
       Paragraph(p1, ps)\ 
       ] 


    data.append(table_row) 
    t1 = Table(data) 
    t1.setStyle(TableStyle([\ 
       ('GRID', (0,0), (-1,-1), 0.25, colors.red, None, (2,2,1)),\ 
      ])) 
    story.append(t1) 

    doc.build(story) 

if __name__ == "__main__": 
    make_report() 

這是字體較大時的兩個問題。

  • 文本較大的電池,以便它超越國界
  • 的間距行間太小

我怎樣才能解決這個問題?

回答

4

這兩個問題實際上是由同一個問題引起的,也就是Paragraph的高度。表格單元由確定線條高度的線條間距決定。同時,由於行間距也導致空白不足。

在Reportlab中,根據文檔使用leading樣式屬性設置行間距。

間間隔(主導)

垂直於其中一個行開始和下一個開始被稱爲前導偏移的點之間的偏移量。

因此你的代碼的正確版本將使用:

ps = ParagraphStyle('title', fontSize=20, leading=24) 

這導致: Example of the output after correction

+0

大。效果很好。謝謝。 – max