换行文本不能与reportlab SimpleDocTemplate一起使用

时间:2012-08-07 05:22:12

标签: reportlab

您好我是reportlab的新手。我想生成一个包含表格的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)

请在此问题上帮助我,并提前致谢

2 个答案:

答案 0 :(得分:2)

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

让我们说,你确实有一个很长的词,如上所示。还有一种方法可以做到这一点。您需要为您的样式定义wordWrap属性。在声明样式后添加以下代码。

styleB.wordWrap = 'CJK'

styleN.wordWrap = 'CJK'

WordWrap设置为'CJK'以获得亚洲语言换行。这个文字包裹只是查找行的结尾并自动进行自动换行。

对于你的解决方案,我将使用wordWrap'LTR'作为文本,并使用'CJK'wordWrap长期永不结束的数字

答案 1 :(得分: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)