图片无法在 table 中呈现,请协助
Image won't render in the table, please assist
我正在尝试将 MathplotLib 生成的图像渲染为 ReportLab 创建的 pdf。请修复。
graph1 = Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png')#,kind='direct',mask='auto',hAlign='CENTER',)
first_graph_table_header = [["Average Annual Medical Cost Per Employee",""],
["",graph1]]
first_graph_page_style = TableStyle([('SPAN', (0, 0), (1, 0))])
# ('ALIGN', (0, 3), (8, 3), 'CENTER'),
# ('ALIGN', (0, 0), (8, 0), 'CENTER'),
# ('VALIGN', (0, 0), (8, 0), 'MIDDLE'),
# ('VALIGN', (2, 3), (6, 3), 'MIDDLE'),
# ('BACKGROUND', (0, 2), (8, 2), colors.HexColor('#305496')),
# ('BACKGROUND', (0, 3), (8, 3), colors.HexColor('#b4c6e7')),
# ('BACKGROUND', (8, 2), (8, 3), colors.orange),
# ('FONT', (0, 2), (-9, -2), 'Helvetica-Bold', 10),
# ('FONT', (2, 3), (-7, -1), 'Helvetica-Bold', 12),
# ('FONT', (3, 3), (-3, -1), 'Helvetica-Bold', 10),
# ('TEXTCOLOR', (0, 2), (-9, -2), colors.white)
# ])
g1 = Table(first_graph_table_header, colWidths=[14.75, 161],
rowHeights=[5.5*mm, 2*inch])
g1.setStyle(first_graph_page_style)
self.story.append(g1)
self.story.append(Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png'))
self.story.append(PageBreak())
# def createGraphs(self, canvas, doc):
# ----------------------------------------------------------------------
if __name__ == "__main__":
t = CoverPage_Index()
t.run()
我不断收到 AttributeError: Image instance has no attribute 'getKeepWithNext'
我遇到了同样的问题。有点疯狂之后,我明白了原因是名字冲突。所以这是对我有用的解决方案。
您需要一个可流动的图像,但名称 Image 在您导入的 classes 中可能不是唯一的。
首先,给你想要使用的图像 class 一个你确定在导入时唯一的名称;我用的是 GNAA(到底谁会写一个 class 命名为 GNAA 的?)。
from reportlab.platypus.flowables import Image as GNAA
然后你就可以用它来挑选一张图片了。在此示例中,图像与 python 程序位于同一目录中。重要提示:使用 jpg,因为 png 可能不起作用:
logo = "logo.jpg"
im = GNAA(logo, width=200, height=38)
story.append(im)
现在图像是正确的,可流动的并且具有所需的所有正确参数。
我没有调查图像的分身在哪里,但这个技巧很管用。
我正在尝试将 MathplotLib 生成的图像渲染为 ReportLab 创建的 pdf。请修复。
graph1 = Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png')#,kind='direct',mask='auto',hAlign='CENTER',)
first_graph_table_header = [["Average Annual Medical Cost Per Employee",""],
["",graph1]]
first_graph_page_style = TableStyle([('SPAN', (0, 0), (1, 0))])
# ('ALIGN', (0, 3), (8, 3), 'CENTER'),
# ('ALIGN', (0, 0), (8, 0), 'CENTER'),
# ('VALIGN', (0, 0), (8, 0), 'MIDDLE'),
# ('VALIGN', (2, 3), (6, 3), 'MIDDLE'),
# ('BACKGROUND', (0, 2), (8, 2), colors.HexColor('#305496')),
# ('BACKGROUND', (0, 3), (8, 3), colors.HexColor('#b4c6e7')),
# ('BACKGROUND', (8, 2), (8, 3), colors.orange),
# ('FONT', (0, 2), (-9, -2), 'Helvetica-Bold', 10),
# ('FONT', (2, 3), (-7, -1), 'Helvetica-Bold', 12),
# ('FONT', (3, 3), (-3, -1), 'Helvetica-Bold', 10),
# ('TEXTCOLOR', (0, 2), (-9, -2), colors.white)
# ])
g1 = Table(first_graph_table_header, colWidths=[14.75, 161],
rowHeights=[5.5*mm, 2*inch])
g1.setStyle(first_graph_page_style)
self.story.append(g1)
self.story.append(Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png'))
self.story.append(PageBreak())
# def createGraphs(self, canvas, doc):
# ----------------------------------------------------------------------
if __name__ == "__main__":
t = CoverPage_Index()
t.run()
我不断收到 AttributeError: Image instance has no attribute 'getKeepWithNext'
我遇到了同样的问题。有点疯狂之后,我明白了原因是名字冲突。所以这是对我有用的解决方案。 您需要一个可流动的图像,但名称 Image 在您导入的 classes 中可能不是唯一的。 首先,给你想要使用的图像 class 一个你确定在导入时唯一的名称;我用的是 GNAA(到底谁会写一个 class 命名为 GNAA 的?)。
from reportlab.platypus.flowables import Image as GNAA
然后你就可以用它来挑选一张图片了。在此示例中,图像与 python 程序位于同一目录中。重要提示:使用 jpg,因为 png 可能不起作用:
logo = "logo.jpg"
im = GNAA(logo, width=200, height=38)
story.append(im)
现在图像是正确的,可流动的并且具有所需的所有正确参数。 我没有调查图像的分身在哪里,但这个技巧很管用。