侧边栏壁纸
博主头像
三味的小站博主等级

世界上没有偶然,有的只是必然的结果。

  • 累计撰写 61 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

HTML转图片

三味线
2023-09-07 / 0 评论 / 0 点赞 / 16 阅读 / 1192 字

pyecharts+snapshot_selenium的方式只能将图表转为图片,html中的其他元素会被忽略,几番查找后发现imgkit效果还可以(win10和centos7测试OK)

首先需安装imgkit工具:https://wkhtmltopdf.org/downloads.html

下载对应系统的包安装

linux下若有依赖包缺失可在此下载:https://pkgs.org/

pip install imgkit

import os
import platform
import imgkit
imgkit_cfg = None
if platform.system() == "Windows":
    # 未加环境变量时需指定路径
    path_wkimg = r"D:\Programs\wkhtmltopdf\bin\wkhtmltoimage.exe"
    imgkit_cfg = imgkit.config(wkhtmltoimage=path_wkimg)
else:
    imgkit_cfg = imgkit.config()
imgkit_options = {"encoding": "UTF-8"}
# 1、将html文件转为图片
imgkit.from_file(
    "1111.html", "helloworld.png", options=imgkit_options, config=imgkit_cfg
)
# 2、从url获取html,再转为图片
imgkit.from_url("https://www.baidu.com", "hello.jpg", config=imgkit_cfg)
# 3、将字符串转为图片
imgkit.from_string("Hello!", "hello.jpg", config=imgkit_cfg)

https://blog.csdn.net/BobYuan888/article/details/108769274

0

评论区