您的位置:首页 > 编程语言 > Python开发

【Python入门】使用ReporLab 画图 PDF存储

2017-02-18 20:16 316 查看
 学习Python在使用ReporLab画图,保存成PDF时候遇到了点问题。记录下来

环境:

开发工具(IDE) : JetBrainsPyCharmCommunityEdition2016.3.2操作系统(System):windows7X64Python:3.6.0

 ProjectA:Asimplepdffile

 Code:
fromreportlab.graphics.shapesimportDrawing,String
fromreportlab.graphicsimportrenderPDF
d=Drawing(100,100)
s=String(50,50,'HelloWorld!',textAnchor='middle')
d.add(s)
renderPDF.drawToFile(d,'hello.pdf','Asimplepdffile')
  Expectedoutput:错误一:找不到ReportLab,未定义错误解决方法: 安装ReportLab Pycharm中安装 ReportLab库的步骤 File->Seting->Project:XXX-> ProjectInterpreter  如图,点击下图“+”进入package选择目录然后点击“installpackage” 进行安装 ReportLab3.3版本 需要安装

VisualC++2015BuildTools

  http://landinghub.visualstudio.com/visual-cpp-build-tools

  href="http://landinghub.visualstudio.com/visual-cpp-build-tools"target=_blank>点击打开链接 静静的等待vc++库安装完成第一个工程的至此解决,可以输出第一个PDF文档

 ProjectB

   Code:
fromreportlab.libimportcolors
fromreportlab.graphics.shapesimport*
fromreportlab.graphicsimportrenderPDF
data=[
#yearMonthPredictedHighLow
(2007,8,113.2,114.2,112.2),
(2007,9,112.8,115.8,109.8),
(2007,10,111.0,116.0,106.0),
(2007,11,109.8,116.8,102.8),
(2007,12,107.3,115.3,99.3),
(2008,1,105.2,114.2,96.2),
]
drawing=Drawing(200,150)
pred=[row[2]-40forrowindata]
high=[row[3]-40forrowindata]
low=[row[4]-40forrowindata]
times=[200*((row[0]+row[1]/12.0)-2007)-110forrowindata]
drawing.add(PolyLine(zip(times,pred),strokeColor=colors.blue))
drawing.add(PolyLine(zip(times,high),strokeColor=colors.red))
drawing.add(PolyLine(zip(times,low),strokeColor=colors.green))
drawing.add(String(65,115,'Sunspots',fontSize=18,fillcolor=colors.red))
renderPDF.drawToFile(drawing,'report1.pdf','Sunspots')
错误信息:关键信息为:zip(times,pred)经查询StackOverflow  Tryusing 
list(zip(...)
 whereyouhave 
zip(...)
即是
修改zip(times,pred)为(list(zip(times,pred))
drawing.add(PolyLine(list(zip(times,pred)),strokeColor=colors.blue))
drawing.add(PolyLine(list(zip(times,high)),strokeColor=colors.red))
drawing.add(PolyLine(list(zip(times,low)),strokeColor=colors.green))
drawing.add(String(65,115,'Sunspots',fontSize=18,fillcolor=colors.red))
至此,可以输出文档
参考资料:1.Python基础教程(第2版.修订版)第21章 项目2:画幅好画 Page:3372. http://stackoverflow.com/questions/31011631/python-2-3-object-of-type-zip-has-no-len
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息