【问题标题】:I need to generate and save output of html/css code in JPG with the help of Python我需要在 Python 的帮助下生成并保存 JPG 格式的 html/css 代码输出
【发布时间】:2021-03-16 13:50:01
【问题描述】:

我想在python的帮助下以jpg格式保存到由html/css代码生成的输出。在谷歌上搜索时,我发现我们可以使用 IPython.display.HTML 来实现这一点。但是这个库只适用于 Python 2.0,将来会被弃用,所以想用 Python 3.0 + 实现这一点,还需要将输出保存为 html+css 代码生成的 JPG 格式。任何人都可以帮忙,如何实现这一点? 下面是我能够在 python 中呈现 html 输出的代码。

    #rendering frontend
    
    from IPython.display import display, Image
    IPython.display.HTML(filename='/content/drive/MyDrive/Analysed/Task2/test_page.html')

其中,test_page.html 文件包含简单的 html css 代码。

<html>
<head>
<link rel="stylesheet" href="mystyle.css">
<title>Hello</title>
<style>
.t{width:100px;height:100px;background-color:green;}
.container{width:1000px;height:700px;border:1px solid black;}
</style>
</head>

<body>
<div class="container">
  <div class="t">
    <p>Hello , How's day ?</p>
  </div>
</div>
</body>
</html>

这是我用上面的代码得到的输出,我想保存在 jpg 中 与蟒蛇。 html+css rendered output

【问题讨论】:

  • 您能帮我们添加代码吗?
  • 是的,我已经添加了代码和输出

标签: python ipython


【解决方案1】:

我不知道你运行的是哪个操作系统,但你可以imgkit 来解决这个问题。
安装: pip3 install --user imgkit

用法

import imgkit
imgkit.from_file("input.html", "output.jpg")

请注意,对于这种方法,您需要有效安装 wkhtmltopdf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多