使用 pyecharts 渲染成图片一直是开发者比较关心的功能,pyecharts提供了 selenium、phantomjs 和 pyppeteer 三种方式。
更多介绍可以学习官方文档:https://pyecharts.org/#/zh-cn/render_images
首先需要安装上snapshot-selenium
pipinstallsnapshot-selenium-ihttp://pypi.douban.com/simple--trusted-hostpypi.douban.com
测试代码如下:
frompyecharts.renderimportmake_snapshotfromsnapshot_seleniumimportsnapshotfrompyechartsimportoptionsasoptsfrompyecharts.chartsimportSankeysankey=Sankey( init_opts=opts.InitOpts( width='1000px', height='600px', bg_color='#fff' ) ) sankey.add( '', nodes, links, node_gap=0, node_width=80, pos_right='5%', node_align='justify', focus_node_adjacency=True, linestyle_opt=opts.LineStyleOpts(curve=0.5, opacity=0.2, color="source"), label_opts=opts.LabelOpts(position='inside', color='white'), itemstyle_opts=opts.ItemStyleOpts(border_color="#fff"), ) print(":".join(["CSDN叶庭云", "https://yetingyun.blog.csdn.net/"])) # sankey.render("./results/009.html")make_snapshot(snapshot, sankey.render(), "Pyecharts生成图片.png")
关键代码:
frompyecharts.renderimportmake_snapshotfromsnapshot_seleniumimportsnapshot# 渲染的html保存为png图片make_snapshot(snapshot, sankey.render(), "Pyecharts生成图片.png")
结果如下: