Python 官方文档:入门教程 => 点击学习
pyecharts的表格组件Table生成的html文件转图片方式 工作中由python处理后的数据需要自动化发送到工作群组第一种:pyecharts自带的snapshot_phantomjs
工作需要将pyecharts的表格组件Table生成的html文件转图片,尝试了三种方式,只有第三种方式成功了
snapshot-phantomjs 是 pyecharts + phantomjs 渲染图片的扩展,支持png\jpeg\gif\pdf\svg等格式
pip install snapshot-phantomjs
from pyecharts import options as optsfrom pyecharts.charts import Tablefrom pyecharts.render import make_snapshotfrom snapshot_phantomjs import snapshotfrom pyecharts.options import ComponentTitleOptstable = Table()headers = ["City name", "Area", "Population", "Annual Rainfall"]rows = [ ["Brisbane", 5905, 1857594, 1146.4], ["Adelaide", 1295, 1158259, 600.5], ["Darwin", 112, 120900, 1714.7], ["Hobart", 1357, 205556, 619.5], ["Sydney", 2058, 4336374, 1214.8], ["Melbourne", 1566, 3806092, 646.9], ["Perth", 5386, 1554769, 869.4],]table.add(headers, rows)table.set_global_opts( title_opts=ComponentTitleOpts(title="Table-基本示例", subtitle="我是副标题支持换行哦"))table.render("table_base.html")
file_path = "{}/".fORMat(os.path.dirname(os.path.abspath("/root/echarts.min.js")))Table(init_opts=opts.InitOpts(js_host=file_path))make_snapshot(snapshot,table.render(),"table0.pdf")
结果仍然报错,TypeError: Table.init() Got an unexpected keyWord argument ‘init_opts’
经查找,发现snapshot_phantomjs支持别的图导出如Bar、Grid、Line等都可以用这种方式,但是Table组件不支持
使用Aspose.Words for Python api。用python读取和操作各种类型文档比如 Microsoft Word(DOC、DOCX、ODT)、PDF和 WEB(HTML、markdown)文档
pip install aspose-words
以jpeg为例
import aspose.words as awdoc = aw.Document("table_base.html")imageOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG)imageOptions.jpeg_quality = 10imageOptions.horizontal_resolution = 72# Save the pages as JPGfor page in range(0, doc.page_count):extractedPage = doc.extract_pages(page, 1)extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.jpg", imageOptions)
结果报错:IndentationError: expected an indented block after ‘for’ statement on line 17
经查找,发现这种方式只适用于文本页面,能用Document类加载的html文件,比如论文很适合。
可以将html转为图片或者pdf,不限制类型
pip install imgkit
`pip install pdfkitimport imgkit path_wkimg = r'D:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe' # 工具路径cfg = imgkit.config(wkhtmltoimage=path_wkimg)#可以修改参数,图片大小、语言等# options={# page-size:""# }# 将html文件转为图片imgkit.from_file('table_base.html', 'hellotable.jpg', config=cfg)
运行结果:Loading page (1/2)
Rendering (2/2)
Done
True
在运行路径下即可找到对应生成的图片
来源地址:https://blog.csdn.net/u010738528/article/details/131966303
--结束END--
本文标题: 三种利用python将html文件转图片的方式
本文链接: https://www.lsjlt.com/news/440733.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0