今天为了往pyecharts产生的html里添加微信二维码费了九牛二虎之力,主要是因为pyecharts的文档非常烂!
- 生成微信二维码(正方形图片最好)如下(链接为我的服务器图片地址):
- https://www.geoseis.cn/wp-content/gallery/pycharm/python.gif

QR = myqr.run(
words="https://geoseis.cn/CoV_en.html", # 可以是字符串,也可以是网址(前面要加http(s)://)
version=1, # 设置容错率为最高
level='H', # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
picture="C:/Users/xuzhen/Pictures/0.jpg", # 将二维码和图片合成
colorized=True, # 彩色二维码
contrast=1.0, #用以调节图片的对比度,1.0 表示原始图片,更小的值表示更低对比度,更大反之。默认为1.0
brightness=1.0, #用来调节图片的亮度,其余用法和取值同上
save_name="C:/Users/xuzhen/Pictures/python.gif", # 保存文件的名字,格式可以是jpg,png,bmp,gif
save_dir=os.getcwd() #控制位置
)
- 二维码上传到服务器,因为HTML是引用图片的服务器地址。这里我上传到了自己的网站,取得路径。 https://www.geoseis.cn/wp-content/gallery/pycharm/python.gif
- image函数添加图片:
def image_base() -> Image:
image = Image()
img_src = (
"https://www.geoseis.cn/wp-content/gallery/pycharm/"
"python.gif"
)
image.add(
src=img_src,
style_opts={"width": "200px", "height": "200px", "style": "margin-top: 10px"},
).set_global_opts(
title_opts=ComponentTitleOpts(title="WeChat QR",title_style={"style": "font-size: 18px; font-weight:bold; color:red"},\
subtitle="Scan to follow the realtime update!",subtitle_style={"style": "font-size: 12px; color:red"})
)
return image
- 添加到page函数(重点是add(image_base(),如果没有这个括号是不行的,pyecharts没有这种实例需要自己琢磨它的用法):
page = (Page(page_title="2019-nCov")
.add(world_map)
.add(total_pie)
.add(area_map)
.add(area_heat_geo)
.add(bar)
.add(big_title)
.add(times)
.add(confirms)
.add(confirms_people)
.add(suspects)
.add(suspects_people)
.add(deads)
.add(deads_people)
.add(heals)
.add(heals_people)
.add(confirm_liquid)
.add(suspect_liquid)
.add(dead_liquid)
.add(heal_liquid)
.add(wc)
.add(image_base())
).render("2019-nCov-RealTime.html")
- 划重点,如何改变图片标题的字体(文档没有说明,需要阅读源码自己琢磨下),源码就在Options下的Charts_options.py:
title_opts=ComponentTitleOpts(title="WeChat QR",title_style={"style": "font-size: 18px; font-weight:bold; color:red"},\
subtitle="Scan to follow the realtime update!",subtitle_style={"style": "font-size: 12px; color:red"})
)
C:\Users\xuzhen\env\pyecharts\Lib\site-packages\pyecharts\options
最后的效果图:
