2012-08-07 78 views
3

嗨我是新来的reportlab。我想生成一个包含表格的pdf报告。表中的一些列文本大于列宽,现在我想根据列宽度来包装文本。换行文本不能与reportlab SimpleDocTemplate

以下是我写

# 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) 

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

回答

1

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

假设你有一个很长的单词,如上所示。还有一种方法可以做到这一点。你需要为你的风格定义一个wordWrap属性。在声明样式后添加下面的代码。

styleB.wordWrap = 'CJK' 

styleN.wordWrap = 'CJK' 

自动换行设置为 '中日韩' 获得亚洲语言linewrapping。这个wordwrap只是查找行结束并自动执行单词换行。

对于您的解决方案,我将使用wordWrap'LTR'作为文本和wordWrap'CJK'的长时间永无止境的编号