换行文本不适用于reportlab SimpleDocTemplate

问题描述 投票:0回答:2

嗨,我是报告实验室的新手。我想生成一份包含表格的 pdf 报告。表格中的某些列文本大于列宽,现在我想根据列宽换行文本。

以下是我写的代码


    # Imports
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    from reportlab.lib.styles import getSampleStyleSheet
    from reportlab.lib.units import inch
    from reportlab.lib import colors
    from reportlab.platypus import Paragraph, Frame, Spacer, Image, Table, TableStyle, SimpleDocTemplate
    from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
    
    # My data
    result1 = [1,2,3,4,5,6000000000000000000000000000000000000000000000000]
    result2 = [10,20,30,40,50,60]
    result3 = [100,200,300,400,500,600000000000000000000000000000000000000000000000000]
    
    # create a list and add the elements of our document (image, paragraphs, table) to it
    story = []
    
    # define the style for our paragraph text
    styles = getSampleStyleSheet()
    styleN = styles['Normal']
    styleN.alignment = TA_CENTER
    styleT = styles['Title']
    styleB = styles["BodyText"]
    styleB.alignment = TA_LEFT
    
    # first add the Title of the report
    pTitle = Paragraph('<font size="30" color="darkblue">Report</font>', styleT)
    story.append(pTitle)
    story.append(Spacer(1, .5*inch))
    
    # User details
    story.append(Paragraph("<font color='darkblue'><b>Name : </b></font>" + "<user name>", styleN))
    story.append(Spacer(1, .1*inch))
    
    story.append(Paragraph("<font color='darkblue'><b>e-mail : </b></font>" + "<user email id>", styleN))
    story.append(Spacer(1, 1*inch))
    
    # Create Table
    tabledata = [[Paragraph('object',styleN),Paragraph('titletitletitletitletitletitletitletitletitletitletitle',styleN),Paragraph('description',styleN),Paragraph('latitude',styleN),Paragraph('longitude',styleN),Paragraph('mywlink',styleN)],
                 [Paragraph(str(x),styleB) for x in result1],[Paragraph(str(x),styleB) for x in result2],[Paragraph(str(x),styleB) for x in result3]]#,
    
    
    colwidths = (80, 100, 100, 75, 75, 100)
    GRID_STYLE = TableStyle(
        [('GRID', (0,0), (-1,-1), 0.25, colors.black),
         ('ALIGN', (1,1), (-1,-1), 'LEFT'),
         ('TEXTCOLOR',(0,0), (-1,0), colors.darkblue)
    ]
        )
    t = Table(tabledata, colwidths, None, style=GRID_STYLE)
    
    
    ##t.setStyle(GRID_STYLE)
    t.hAlign='LEFT'
    story.append(t)
    story.append(Spacer(1,.5*inch))
    
    #build our document with the list of flowables we put together
    doc = SimpleDocTemplate('myReport.pdf',pagesize = letter, leftMargin=0.5*inch, topMargin=0.3*inch)
    doc.build(story)

请帮助我解决这个问题,并提前致谢

reportlab
2个回答
2
投票

一般来说,段落本身实际上是一个“自动换行”。如果您使用更现实的测试方法,例如使用 Loren Ipsum 文本。然后所有文本都将在列的宽度内。所以这么长的字符串是不实用的。

假设您确实有一个很长的单词,如上所示。还是有办法做到这一点。您需要为您的样式定义一个 wordWrap 属性。在声明样式后添加以下代码。

styleB.wordWrap = 'CJK'

styleN.wordWrap = 'CJK'

WordWrap 设置为“CJK”以获得亚洲语言换行。这个自动换行只是寻找行尾并自动换行。

对于您的解决方案,我将使用 wordWrap 'LTR' 表示文本,并使用 wordWrap 'CJK' 来表示长的永无止境的数字


0
投票
#Following code will work with SimpleDocTemplate,Table, Paragraph

from reportlab.platypus import SimpleDocTemplate 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle`

#Below 2 line code is important in wrapping text

styles = getSampleStyleSheet() 
styles.add(ParagraphStyle(name="ParagraphStyleName",alignment=TA_JUSTIFY,fontName= 
"Your-Font Name"))



#If your reading data from csv and adding to table follow the below code.
#or
#You can add your data to paragraph as shown below with paragraph style.
#Encoding used for mycode you can negelct it.

main_table_data=[]
with open("preinspection_checklist.csv",encoding="utf8") as f:
        csvdata=csv.reader(f,delimiter=",")
        for row in csvdata:
        

             data=[
                Paragraph(row[0],styles["ParagraphStyleName"]),
                Paragraph(row[1],styles["ParagraphStyleName"]),
                Paragraph(row[2],styles["ParagraphStyleName"]),
               Paragraph(row[3],styles["ParagraphStyleName"]),
               Paragraph(row[4],styles["ParagraphStyleName"])
             ]

        
        
          main_table_data.append(data)



elements=[]
main_table=Table(main_table_data,colWidths= your_column_width,rowHeights= 
your_row_height,repeatRows=1)

#Here define your table style

main_table.setStyle(your_main_table_style)
elements.append(main_table)
pdf.build(elements)
© www.soinside.com 2019 - 2024. All rights reserved.