我正在尝试使用2个数据感知图制作单个pdf。一个是从表1中获取数据的条形图,另一个是从另一个表中获取数据的字符串(文本框类型的东西)。 当我在reportlab中运行这个程序时,后面调用的字符串部分(具有将字符串连接到数据库并显示它的代码的类)将覆盖条形图。 我没有找到任何方法在一个pdf中一起显示它们。 请建议。
from rlextra.graphics.guiedit.datacharts import DataAwareDrawing, ODBCDataSource, DataAssociation
from reportlab.graphics.charts.barcharts import VerticalBarChart3D
from reportlab.graphics.shapes import _DrawingEditorMixin, Line, String
from reportlab.lib.colors import red
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
class avg_max_min(_DrawingEditorMixin,DataAwareDrawing):
def __init__(self,width=1000,height=1000,*args,**kw):
DataAwareDrawing.__init__(self,width,height,*args,**kw)
self._add(self,Line(750,900,900,900),name='horline1',validate=None,desc=None)
self._add(self,Line(750,930,900,930),name='horline2',validate=None,desc=None)
self._add(self,Line(750,960,900,960),name='horline3',validate=None,desc=None)
self._add(self,Line(750,900,750,960),name='verline1',validate=None,desc=None)
self._add(self,Line(800,900,800,960),name='verline2',validate=None,desc=None)
self._add(self,Line(850,900,850,960),name='verline3',validate=None,desc=None)
self._add(self,Line(900,900,900,960),name='verline4',validate=None,desc=None)
self._add(self,String(760,945,'Average'),name='label1',validate=None,desc=None)
self.label1.fontSize = 8
self.label1.fillColor = red
self.label1.textAnchor = 'start'
self._add(self,String(810,945,'Minimum'),name='label2',validate=None,desc=None)
self.label2.fillColor = red
self.label2.fontSize = 8
self.label2.textAnchor = 'start'
self._add(self,String(860,945,'Maximum'),name='label3',validate=None,desc=None)
self.label3.fillColor = red
self.label3.fontSize = 8
self.label2.textAnchor = 'start'
self._add(self,String(760,915,'text'),name='label4',validate=None,desc=None)
self._add(self,String(810,915,'text'),name='label5',validate=None,desc=None)
self._add(self,String(860,915,'text'),name='label6',validate=None,desc=None)
self.dataSource = ODBCDataSource()
self.dataSource.driver = 'mysql'
self.dataSource.name = 'view'
self.dataSource.user = 'root'
self.dataSource.password = 'sou'
self.dataSource.sql = 'select cast(avg(diff) as char(50)), cast(min(diff) as char(50)),cast(max(diff) as char(50)) from new_table'
self.dataSource.associations.size = 3
self.dataSource.associations.element00 = DataAssociation(column=0, target='label4.text', assocType='scalar')
self.dataSource.associations.element01 = DataAssociation(column=1, target='label5.text', assocType='scalar')
self.dataSource.associations.element02 = DataAssociation(column=2, target='label6.text', assocType='scalar')
class final_logon(_DrawingEditorMixin,DataAwareDrawing):
def __init__(self,width=1000,height=1000,*args,**kw):
DataAwareDrawing.__init__(self,width,height,*args,**kw)
self._add(self,VerticalBarChart3D(),name='vertical',validate=None,desc=None)
self.vertical.x = 150
self.vertical.y = 200
self.vertical.width = 600
self.vertical.height = 600
self.dataSource = ODBCDataSource()
self.dataSource.driver = 'mysql'
self.dataSource.name = 'view'
self.dataSource.user = 'root'
self.dataSource.password = 'sou'
self.dataSource.sql = 'select id, range_value, user_number from final_logon'
self.dataSource.associations.size = 3
self.dataSource.associations.element00 = DataAssociation(column=0, target='verticalId', assocType='scalar')
self.dataSource.associations.element02 = DataAssociation(column=1, target='vertical.categoryAxis.categoryNames', assocType='vector')
self.dataSource.associations.element01 = DataAssociation(column=2, target='vertical.data', assocType='tmatrix')
doc = SimpleDocTemplate("merged.pdf",pagesize=letter,
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18)
Story = []
Story.append(avg_max_min().go())
Story.append(final_logon().go())
doc.build(Story)
我收到的错误:
C:\Python26\to be merged>merged.py
Traceback (most recent call last):
File "C:\Python26\to be merged\merged.py", line 96, in <module>
doc.build(Story)
File "C:\Python26\lib\site-packages\reportlab\platypus\doctemplate.py", line 1
117, in build
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
File "C:\Python26\lib\site-packages\reportlab\platypus\doctemplate.py", line 8
80, in build
self.handle_flowable(flowables)
File "C:\Python26\lib\site-packages\reportlab\platypus\doctemplate.py", line 7
44, in handle_flowable
self.handle_keepWithNext(flowables)
File "C:\Python26\lib\site-packages\reportlab\platypus\doctemplate.py", line 7
11, in handle_keepWithNext
while i<n and flowables[i].getKeepWithNext(): i += 1
AttributeError: 'NoneType' object has no attribute 'getKeepWithNext'
答案 0 :(得分:0)
之前我没有使用过rlextra
模块,但是据我所知,发生的事情是,ReportLab的任何部分都没有真正设计为附加到现有文件。相反,您需要做的是生成这两个图像(如果您想避免将它们写入实际文件,请考虑使用StringIO
),然后将它们一起绘制在一个画布上。