在Reportgraph的同一行中,Reportlab有两个项目

时间:2016-06-08 08:52:30

标签: python django python-3.x pdf-generation reportlab

我想在左边和右边的同一行中对齐两个文字:

TEXT1 TEXT2

并在同一行中左对齐图像并居中文本:

图片文字

怎么可能?

这是我的代码:

Story=[]

styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
styles.add(ParagraphStyle(name='Center', alignment=TA_CENTER))
styles.add(ParagraphStyle(name='Left', alignment=TA_LEFT))
styles.add(ParagraphStyle(name='Right', alignment=TA_RIGHT))


ptext = '<font size=15><b>{0}</b></font>'.format("TITLE")
Story.append(Paragraph(ptext, styles["Center"]))

Story.append(Spacer(10, 20))

ptext = '<font size=10>TEXT1 </font>'
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))

ptext = '<font size=10>CODICE OPERATORE</font>'
Story.append(Paragraph(ptext, styles["Normal"]))

Story.append(Spacer(1, 12))
Story.append(Spacer(1, 12))



signature = os.path.join(settings.MEDIA_ROOT, user.attr.get("signature"))
im = Image(signature, 80, 80)
im.hAlign ='RIGHT'

ptext = '<font size=10>Firma</font>'

Story.append(Spacer(1, 12))
Story.append(Spacer(1, 12))

Story.append(Paragraph(ptext, styles["Right"]))
Story.append(Spacer(1, 12))
Story.append(im)

Story.append(PageBreak())

doc.build(Story)

谢谢

1 个答案:

答案 0 :(得分:5)

使用Table。该表可以有自己的样式,项目(ParagraphImage,...)也可以有自己的样式,这样你就可以在表格单元格中有不同的对齐项目

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_RIGHT

styles = getSampleStyleSheet()
style_right = ParagraphStyle(name='right', parent=styles['Normal'], alignment=TA_RIGHT)
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4)
Story=[]
# here you add your rows and columns, these can be platypus objects
tbl_data = [
    [Paragraph("Hello", styles["Normal"]), Paragraph("World (right)", style_right)],
    [Paragraph("Another", styles["Normal"]), Paragraph("Row (normal)", styles["Normal"])]
]
tbl = Table(tbl_data)
Story.append(tbl)
doc.build(Story)