当前位置:   article > 正文

python 使用 html2image 将 html 转图片_python html2image

python html2image

前置准备

环境系统: centos7
安装 conda

wget https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
bash Anaconda-latest-Linux-x86_64.sh
source ~/.bashrc
  • 1
  • 2
  • 3

安装环境

conda create -n py311 python=3.11.5
conda activate py311
python -m pip install html2image
yum install chromium libexif
  • 1
  • 2
  • 3
  • 4

脚本

import os
from html2image import Html2Image


def ImportImageFromExecl(filepath, outputPNGImage):
    hti = Html2Image(custom_flags=['--no-sandbox'], size=(900, 1220))
    hti.screenshot(
        html_file=filepath,
        save_as=outputPNGImage
    )
    
filepath = "test.html"
outputPNGImage = 'test.png'

ImportImageFromExecl(filepath, outputPNGImage)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

FAQ

启动环境编码失败 UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xbb’ in position 141:

export PYTHONIOENCODING=utf_8
source activate {env]
  • 1
  • 2

ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported

# 需要加上 -no-sandbox 参数
hti = Html2Image(custom_flags=['--no-sandbox'], size=(900, 1220))
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/611060
推荐阅读
相关标签
  

闽ICP备14008679号