使用reportlab将背景图像添加到EPS

时间:2018-07-18 11:27:29

标签: python reportlab

我正在使用reportlab创建图表。所以我想生成一个eps格式的文档,在其中我想在图表中添加背景图像。当我尝试将背景图像添加到文档中而不是作为背景时,它会堆积在图表下方。 当我为pdf运行时,使用相同的逻辑就可以了。为了找出问题,我查看了reportlab库中的renderPS.py和renderPDF.py,这两个文件分别用于创建EPS和PDF。 在renderPDF中,我们使用 drawInlineImage()在图表中生成内嵌图像,但renderPS不存在该图像。 该路径是正确的,因为我可以对PDF进行同样的操作,并且图像大小也正确。我也无法将drawInlineImage函数添加到renderPS.py。

import reportlab
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin,Image
from reportlab.lib.colors import blue, PCMYKColor
from reportlab.graphics.charts.textlabels import LabelOffset
from reportlab.graphics.charts.legends import Legend

class VBarChartWLineBarLabels01(_DrawingEditorMixin,Drawing):
    def __init__(self,width=200,height=300,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None)
        self._add(self,Legend(),name='legend',validate=None,desc=None)
        self.legend.columnMaximum    = 2
        self.legend.alignment='right'
        self.legend.dx               = 6
        self.legend.dy               = 6
        self.legend.dxTextSpace      = 5
        self.legend.deltay           = 10
        self.legend.strokeWidth      = 0
        self.legend.strokeColor      = None
        self.legend.subCols[0].minWidth = 75
        self.legend.subCols[0].align = 'left'
        self.legend.subCols[1].minWidth = 25
        self.legend.subCols[1].align = 'right'
        self.legend.boxAnchor      = 'c'
        self.legend.y              = 150
        self.legend.colorNamePairs = [(PCMYKColor(0,54,100,10,alpha=100), ('1985')),(PCMYKColor(100,60,0,50,alpha=100), ('2035'))]
        self.width       = 400
        self.legend.x              = 440
        self.chart.y = 30
        self.chart.x = 35
        self.chart.width=self.width-self.chart.x - 60
        self.chart.height=self.height-2*self.chart.y
        self.chart.valueAxis.drawGridLast = False
        self.chart.categoryAxis.drawGridLast = False
        self.chart.valueAxis.valueMax = 30
        self.chart.valueAxis.valueMin = 0
        self.chart.valueAxis.valueStep = 5
        self.chart.valueAxis.gridStrokeDashArray = None
        self.chart.valueAxis.gridEnd = self.width-60
        self.chart.valueAxis.gridStart = 35
        self.chart.valueAxis.visibleGrid =1
        self.chart.valueAxis.labelTextFormat = '%s%%'
        self.chart.valueAxis.maximumTicks = 20
        self.chart.valueAxis.minimumTickSpacing = 10
        self.chart.valueAxis.gridStrokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.valueAxis.labels.dx = -10
        self.chart.barWidth = 3
        self.chart.groupSpacing = 5
        self.chart.data = [(15,17,14,12,15),(23,26,25,23,23)]
        self.chart.bars[0].fillColor   = PCMYKColor(0,54,100,10,alpha=100)
        self.chart.bars[1].fillColor   = PCMYKColor(100,90,0,50,alpha=100)
        self.chart.categoryAxis.categoryNames = ["England","Wales","Scotland","Northern\nIreland","UK"]
        self.chart.categoryAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.categoryAxis.labels.textAnchor='middle'
        self.chart.valueAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
        self.chart.bars.strokeColor = None
        self.chart.bars.strokeWidth = 0
        inPath = 'gepgraphic-allocation-piechart-map.jpg'
        img = Image(0, 0, self.chart.width, self.chart.height, inPath)
        self.chart.background = img
if __name__=="__main__": #NORUNTESTS
    VBarChartWLineBarLabels01().save(formats=['eps'],outDir='.',fnRoot=None)

这里是上述问题的代码,它也适用于pdf,也仅适用于格式= ['eps']的最后一行代码,我们需要用pdf替换eps并尝试运行我想要的文件生成的代码它可以生成相同的eps格式。

1 个答案:

答案 0 :(得分:1)

这是reportlab的错误,问题在于 renderPS.py 文件。 这是您需要在函数 def drawImage(self,image) def drawImage(self,image,x1,y1,width = None,height = None)中更新的代码:< / strong>分别在_PSRenderer(Renderer) PSCanvas 类中。

def drawImage(self, image):
        from reportlab.lib.utils import ImageReader
        im = ImageReader(image.path)
        self._canvas.drawImage(im._image,image.x,image.y,image.width,image.height) 


def drawImage(self, image, x1,y1, width=None,height=None): # Postscript Level2 version
        # select between postscript level 1 or level 2
        if self.PostScriptLevel==1:
            self._drawImageLevel1(image, x1,y1, width, height)
        elif self.PostScriptLevel==2:
            self._drawImageLevel2(image, x1, y1, width, height)
        else :
            raise ValueError('Unsupported Postscript Level %s' % self.PostScriptLevel)

注意:它适用于reportlab版本2.5