简体   繁体   English

Reportlab。 带两列的浮动文本

[英]Reportlab. Floating Text with two Columns

First of all, I'm new to python, reportlab, xhtml2pdf. 首先,我是python,reportlab,xhtml2pdf的新手。 I've already done my first pdf files with reportlab, but I ran into the following problem. 我已经使用reportlab完成了第一个pdf文件,但是遇到了以下问题。

I need a large text in two columns. 我需要在两列中写一个大文本。

First I create my canvas, create my story, append my large text as a paragraph to the story, create my Frame and finally add the story to the frame. 首先,我创建画布,创建故事,将大文本作为段落添加到故事中,创建框架,最后将故事添加到框架中。

c = Canvas("local.pdf")
storyExample = []
textExample = (""" This is a very large text Lorem Ipsum ... """)
storyExample.append(Paragraph(textExample, styleText))
frameExample = Frame(0, 0, 50, 50,showBoundary=0)
frameExample.addFromList(storyExample,c)
c.showPage()
c.save()

Works like a charm. 奇迹般有效。 But I need to show the text in a two column represantation. 但是我需要以两列表示形式显示文本。

Now the text just flows threw my frame like: 现在,文字就像我的框架一样流动:

|aaaaaaaaaaaaaaaaaaaa|
|bbbbbbbbbbbbbbbbbbbb|
|cccccccccccccccccccc|
|dddddddddddddddddddd|

But I need it like this: 但我需要这样:

|aaaaaaaaa  bbbbbbbbbb|
|aaaaaaaaa  cccccccccc|
|bbbbbbbbb  cccccccccc|
|bbbbbbbbb  dddddddddd|

I hope you understood what I am trying to say. 希望您理解我要说的话。

This can be done using BaseDocTemplate and Frame as you can read here . 您可以在此处阅读使用BaseDocTemplateFrame来完成。 I modified that receipe to only use a two frame layout: 我修改了收据以仅使用两个框架的布局:

from reportlab.platypus import BaseDocTemplate, Frame, Paragraph, PageBreak, PageTemplate
from reportlab.lib.styles import getSampleStyleSheet
import random

words = "lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore et".split()

styles=getSampleStyleSheet()
Elements=[]

doc = BaseDocTemplate('basedoc.pdf',showBoundary=1)

#Two Columns
frame1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='col1')
frame2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='col2')

Elements.append(Paragraph(" ".join([random.choice(words) for i in range(1000)]),styles['Normal']))
doc.addPageTemplates([PageTemplate(id='TwoCol',frames=[frame1,frame2]), ])


#start the construction of the pdf
doc.build(Elements)

If you want to do this in plain ReportLab you'll have to figure out where to break the paragraph yourself. 如果要在普通ReportLab中执行此操作,则必须自己弄清楚该段落的位置。 If you instead use Platypus to set up a document class, you can specify the frames to lay text out in on the page and the order of the frames will determine where things flow over to. 如果改用Platypus来设置文档类,则可以指定框架以在页面上放置文本,框架的顺序将决定流向何处。 When the paragraph reaches the end of the first frame on the left of the page, the content will automatically flow over into the next frame, which you can position on the right of the page to achieve what you want. 当段落到达页面左侧第一帧的末尾时,内容将自动流到下一帧,您可以将其定位在页面右侧以实现所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM