ReportLab 是否有可流动的 matplotlib?
我想将 matplotlib 图表直接嵌入到 ReportLab 生成的 PDF 中 - 即不先保存为 PNG,然后将 PNG 嵌入到 PDF 中(我想我会得到更好质量的输出)。
有谁知道ReportLab是否有matplotlib flowable?
谢谢
I want to embed matplotlib charts into PDFs generated by ReportLab directly - i.e. not saving as a PNG first and then embedding the PNG into the PDF (i think I'll get better quality output).
Does anyone know if there's a matplotlib flowable for ReportLab?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是使用 pdfrw 的解决方案:
Here's a solution using pdfrw:
Patrick Maupin,pdfrw 的作者,在另一个< href="http://stackoverflow.com/a/32021013/3577601" title="question">问题。 (我感谢他对我之前的回答的善意之言。)他还提到,在使用 pdfrw 提取数据之前将 matplotlib 数据保存到多页 PDF 中,可以通过减少重复资源来减少最终的 reportlab PDF 的大小。下面是对其代码示例的修改,演示了如何通过首先写入多页 matplotlib PDF 来减小 PDF 文件大小。对于本示例,文件大小减少了大约 80%。
注意:这是专门用于 matplotlib 图形的。
由于 matplotlib 的 PdfPages 仅将文件路径作为输入,因此 PdfImageCache 对象将多页 PDF 写入临时文件。试图在记忆中做到这一点需要做更多的工作。
Patrick Maupin, the author of pdfrw, provided a simpler, less complicated answer in another question. (My thanks for his kind words about my previous answer.) He also mentioned that saving matplotlib figures to a multi-page PDF before using pdfrw to extract them would reduce the size of the final reportlab PDF by reducing duplicate resources. So here's a modification of his code example that demonstrates how the PDF file size is reduced by writing to a multi-page matplotlib PDF first. For this example, the file size is reduced by about 80%.
Note: This is specialized for use with matplotlib figures.
Since matplotlib's PdfPages only takes a file path as input, the PdfImageCache object writes the multi-page PDF to a temporary file. Trying to do it in memory would take a lot more work.
没有,但我自己使用 MatPlotLib 和 ReportLab 时所做的就是生成 PNG,然后嵌入 PNG,这样我就不需要使用 PIL。但是,如果您确实使用 PIL,我相信您应该能够使用 MatPlotLib 和 ReportLab 生成并嵌入 EPS。
There is not one, but what I do in my own use of MatPlotLib with ReportLab is to generate PNGs and then embed the PNGs so that I don't need to also use PIL. However, if you do use PIL, I believe you should be able to generate and embed EPS using MatPlotLib and ReportLab.
Python 3 的解决方案,并将 matplotlib 图嵌入为矢量图像(无光栅化)
svglib
。Conda-forge 提供了
Solution for Python 3, and embedding matplotlib figure as a vector image (no rasterization)
svglib
is available from conda-forge.我创建了一个 Flowable 示例,用于与 ReportLab 的商业 RML 模板语言一起使用。这里的示例非常有帮助,但需要稍加调整才能作为 RMLplugInFlow 元素工作。
GitHub 上提供了一个工作示例。
这是 Flowable 本身:
I've created an example Flowable for use with ReportLab's commercial RML templating language. The examples here are extremely helpful but need a slight twist to work as an RML plugInFlow element.
A working example is available on GitHub.
Here's the Flowable itself: