4

嗨,我是报告实验室的新手。我想生成一个包含表格的 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)

请在这个问题上帮助我,并在此先感谢

4

2 回答 2

2

一般来说,Paragraph 本身实际上是一个“文字包装”。如果您使用更现实的测试方法,例如,使用Loren Ipsum Texts。然后所有文本都将在列的宽度内。因此,具有如此长字符串的单词是不实用的。

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

styleB.wordWrap = 'CJK'

或者

styleN.wordWrap = 'CJK'

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

对于您的解决方案,我将使用 wordWrap 'LTR' 作为文本和 wordWrap 使用 'CJK' 的长且永无止境的数字

于 2013-03-13T10:13:09.133 回答
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)
于 2021-05-22T14:49:21.850 回答