使用python从Excel导出到PDF:获取分割图

2024-10-03 02:47:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在将EXCEL(包含文本和图形)转换为PDF时,我得到了以下PDF报告(一个图形分为两个不同的页面)。 实际值: sp1

sp2

期望值: orig

“代码”

    def make_plot(self,chart_data):
        #see report function: add_chart(report,chart_data) that built chart_data
        try:
            subplot(111)
            subplots_adjust(bottom=0.3)
            file = tempfile.mktemp(suffix=".png")
            clf() #clear plot data
            title(chart_data[1][0])
            xlabel(chart_data[1][1])
            ylabel(chart_data[1][2])
            x=chart_data[2]
            xcount=len(chart_data[2])
            ycount=xcount-3
            y = list()
            iter=0

            for ydata in chart_data:
                if iter>=3:  #y data starts on 3 array
                    y=ydata[2]
                    plot(x,y,color=chart_data[iter][1], label=chart_data[iter][0])
##                    y[:] = []
                iter=iter+1

            legend(loc=(-0.15,-0.35))
            savefig(file, bbox_inches="tight", facecolor='w', dpi=120)
            self.insert_image(file) #insert image from file into Excel
        except:
            ErrStr = "Error: make_plot(self,chart_data), " + chart_data[1][0]
            print ErrStr
            self.insert_comment(["COMMENT",ErrStr])
            print sys.exc_info()
            self.pass_status = 0
        finally:
            if(os.path.exists(file)):
                os.remove(file)

def add_chart(report,chart_data):
    #report created by ReportSetupTest("name",list)  dict [TestName][pf]
    #add_chart(report,[["Title","X Axis","Y Axis"], [x1,x2,xn],["ledged 1","color 1",[y1,y2,yn]],#["ledged 2","color 2",[2y1,2y2,2yn]],["ledged n","color n",[ny1,ny2,nyn]]])
    chartlist =list()
    chartlist=["CHART"]
    for dat in chart_data:
        chartlist.append(dat)
    report[len(report) - 1]['pf'].append(chartlist)

如何使实际如预期?是因为代码问题还是pdf问题?图像在EXCEL中正确显示,但问题只出现在PDF中。有人能建议我怎么做吗?你知道吗


Tags: selfreportadddatapdfplotchartlist