如何使用 ReportLab 将两个图表合并为一个 PDF?

发布于 2025-01-06 19:23:26 字数 1006 浏览 1 评论 0原文

问题:我正在使用 ReportLab 生成图表。图表已正确生成,但生成为不同的 PDF。我想将它们合并成一个现有的 pdf 文件。

代码的基本结构是

class BreakdownPieDrawing():

    def firstChart():
        #code for generating first Pie chart

    def secondChart():
        #code for generating second Pie chart

if __name__=="__main__":   
    drawing1 = BreakdownPieDrawing()  
    drawing1.firstChart()
    drawing1.save(formats=['pdf'],outDir='.',fnRoot='first')

    drawing2 = BreakdownPieDrawing()  
    drawing2.secondChart()
    drawing2.save(formats=['pdf'],outDir='.',fnRoot='second')

完整代码片段请参阅http://www.reportlab.com/snippets/4 /

此代码生成两个单独的 PDF。我怎样才能将它们合并成一个PDF文件?

我尝试编码:

def makePdf(self,drawing):  
    doc = SimpleDocTemplate('hello.pdf')
    doc.build(drawing)

然后将“BreakdownPieDrawing”类的对象传递到此方法中。但这种方法行不通。
我是 reportLab 和 python 的新手,所以请原谅我这么难看的代码。

所以问题是如何将此图表添加到现有的 pdf 中。任何帮助将不胜感激。

Question : I'm generating charts using ReportLab. Charts are generated properly but into different PDF. I want to combine them into a single existing pdf.

basic structure of code is

class BreakdownPieDrawing():

    def firstChart():
        #code for generating first Pie chart

    def secondChart():
        #code for generating second Pie chart

if __name__=="__main__":   
    drawing1 = BreakdownPieDrawing()  
    drawing1.firstChart()
    drawing1.save(formats=['pdf'],outDir='.',fnRoot='first')

    drawing2 = BreakdownPieDrawing()  
    drawing2.secondChart()
    drawing2.save(formats=['pdf'],outDir='.',fnRoot='second')

for full code Snippets please refer http://www.reportlab.com/snippets/4/

This code produces two separate PDFs. How can i combine them into single PDF.

I tried this to code :

def makePdf(self,drawing):  
    doc = SimpleDocTemplate('hello.pdf')
    doc.build(drawing)

and then after I'm passing "BreakdownPieDrawing" class's object into this method. But this approach is not working.
I'm new to reportLab and python so pardon me for such a ugly code.

So the question is how to add this charts into existing pdf. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2025-01-13 19:23:26

如果您仔细查看您提供的代码片段(免责声明:我写的;-)),特别是在第 33 行,您会看到

# adding a pie chart to the drawing   
self._add(self,Pie(),name='pie',validate=None,desc=None)

该行将饼图添加到绘图中,然后您需要将第二个图表添加到您的同一绘图中使用 _add 方法,再次像第 46 行的代码片段那样添加图例。您的绘图将包含两个图表,当您将其另存为 PDF 时,您应该会同时获得这两个图表。

If you look closely at the code snippet you provided (disclaimer: I wrote ;-) ) specifically at line#33 you see

# adding a pie chart to the drawing   
self._add(self,Pie(),name='pie',validate=None,desc=None)

That lines adds the Pie chart to the drawing, you will then need to add your second chart to the same drawing you use the _add method, again like the snippet does on line#46 for adding the legend. Your drawing will have the two charts and when you save it as PDF you should get them both.

ヅ她的身影、若隐若现 2025-01-13 19:23:26

这两个图表实际上是整页吗?如果是这样,您可以在绘制两者之间调用分页符。否则,您只需调整坐标,使两者都适合页面即可。

或者,如果您正在做一些更复杂的事情并且最终将使用 Platypus,那么创建一个自定义 Flowable 来渲染它们可能是有意义的。

Are these two charts actually full page? If so, you can just call for a page break between drawing the two. Otherwise you can just adjust the coordinates so that both will fit on the page.

Alternatively, if you are doing something more complicated and will end up using Platypus, it might make sense to create a custom Flowable to render them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文