- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 drawImage 方法将使用 matplotlib 生成的图形添加到 reportlab Canvas ,而不必先将图形保存到硬盘驱动器。
我的问题是: Is there a matplotlib flowable for ReportLab? ,这很好地解决了。但是,我不想使用 DocTemplates、Stories、Flowables 等。如前所述,我想使用 drawImage 将它放在 Canvas 中的某个位置。
我尝试使用以下方法将 matplotlib 图转换为 PIL 图像:
2) http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
例如,一些无法工作的代码是:
import Image
import matplotlib.pyplot as plt
import cStringIO
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
fig = plt.figure(figsize=(4, 3))
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
imgdata = cStringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0) # rewind the data
im = Image.open(imgdata)
c = canvas.Canvas('test.pdf')
#c.drawImage(imgdata, cm, cm, inch, inch)
c.drawImage(im, cm, cm, inch, inch)
c.save()
尝试绘制 imgdata
导致错误:
AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'
在绘制 im
时给出:
AttributeError: rfind
现在有人如何解决这个问题?任何帮助将不胜感激。
最佳答案
问题是 drawImage 需要 ImageReader 对象或文件路径,而不是文件句柄。
以下应该有效:
import Image
import matplotlib.pyplot as plt
import cStringIO
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
from reportlab.lib.utils import ImageReader
fig = plt.figure(figsize=(4, 3))
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
imgdata = cStringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0) # rewind the data
Image = ImageReader(imgdata)
c = canvas.Canvas('test.pdf')
c.drawImage(Image, cm, cm, inch, inch)
c.save()
关于python - 如何在 reportlab Canvas 中绘制 matplotlib 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18897511/
ReportLab 用户指南中说: The colortest.py script in reportlab/demos/colors demonstrates thedifferent ways i
ReportLab 用户指南中说: The colortest.py script in reportlab/demos/colors demonstrates thedifferent ways i
我正在使用 ReportLab VerticalBarChart 生成条形图。我需要其中一个条(每次都是不同的)与其他条的颜色不同,以指定它是“选择”值。 否则,我需要以与其他栏不同的方式标记它。 知
我正在使用 ReportLab 生成 PDF 报告。 我使用了 SimpleDocTemplate 并设置了 pageSize = A5。但是在构建PDF后,当我打开创建的文件时,页面大小为A4。 d
是否可以在段落样式中设置字母/字符间距?我尝试使用spaceShrinkage但这没有任何区别 最佳答案 根据official documentation ,p。 30: The setCharSpa
我的销售订单模板中有以下 rml: [[ repeatIn(o.order_line, 'l') ]] [[ l.name ]] [[
你好, 我正在使用报告实验室生成 pdf。我想绘制一个文本,它是一个 Paragraph,代表一个标题,因此,如果标题比段落的可用长度长,它将在下一行拆分,如果标题太长适合段落的可用宽度和高度,然后调
如何设置表格的左侧位置? response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = '
在报告实验室中 user guide (第 79 页),这是将图像插入表格的方式。 I = Image('../images/replogo.gif') I.drawHeight = 1.25*inc
reportlab文档中有LINEBELOW和LINEABOVE。但我找不到定义右边界或左边界(LINELEFT、LINERIGHT)的方法。 关于如何渲染只有右边框的单元格有什么建议吗? 编辑: 我
有什么方法可以在 reportlab 中获取 Flowable Paragraph 的行数?我有一个很长的字符串,用不同的大小和字体打印。我需要知道要使用 TA_JUSTIFY 对齐打印的整个段落使用
在文档和源代码中,我看到很多对圆角矩形的引用,但是,我似乎无法绘制一个。 我正在绘制一个普通的矩形: canvas.rect(15, 625, 250, 125, fill=0) 我向圆角传递哪些参数
我正在处理发票,我只想在最后一页(也可以是第一页)添加页脚。 由于表的数据是动态的,我无法计算页数。 现在我正在使用 2 个页面模板,第一页(带有 2 个框架和页脚 1)和下一页(带有 1 个框架和页
我知道 LINEABOVE 和 LINEBELOW 样式,我想知道是否有一种方法可以在表格中绘制一条指定宽度的线。 我正在尝试添加一条不“接触”表格边框的线,如果我可以在单元格之间添加一些填充,LIN
我快速浏览了一下网络,但未能找到一种在 Python 中使用 ReportLab 生成 PDF 时设置背景颜色的方法。如何设置背景颜色? 最佳答案 不要使用复杂的方法,而是使用这个简单的技巧: im
我想知道如何将我的脚本保存到桌面。这是我的代码: from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import let
您好,我是报告实验室的新手。我想生成一份包含表格的 pdf 报告。表格中的某些列文本大于列宽,现在我想根据列宽换行文本。 以下是我编写的代码 # Imports from reportlab.pdfg
我正在使用XPreformatted打印一些预格式化的文本,我有一个 换行符的问题。 换行符正确翻译,但另外我得到一个 每行末尾都有“问号”。 这是我的输出: first line? second l
我知道 LINEABOVE 和 LINEBELOW 样式,我想知道是否有一种方法可以在表格中绘制一条指定宽度的线。 我正在尝试添加一条不“接触”表格边框的线,如果我可以在单元格之间添加一些填充,LIN
我快速浏览了一下网络,但未能找到一种在 Python 中使用 ReportLab 生成 PDF 时设置背景颜色的方法。如何设置背景颜色? 最佳答案 不要使用复杂的方法,而是使用这个简单的技巧: im
我是一名优秀的程序员,十分优秀!