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)
请帮我在这个问题上,并在此先感谢