在Colab中将HTML文件转换为png

2024-10-05 12:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将使用Folium创建的html文件转换为png,最终将它们转换为单个gif格式

我一直坚持把html转换成图像。到目前为止,我已经尝试(在Colab上):

代码1:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get("https://www.webite-url.com")
import os
import imageio
import webbrowser

错误1:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

代码2:

import os
import subprocess
url="osm1.html"
outfn = "outfig.png"
subprocess.check_call(["{}".format(url), "--out={}".format(outfn)])

错误2:

FileNotFoundError: [Errno 2] No such file or directory: 'osm1.html': 'osm1.html'

(osm1.html位于Colab的根目录中)

代码3:

!pip install bokeh
import bokeh
from bokeh.io import export_png
url="osm1.html"
export_png(url, filename="plot.png")

错误3:

ValueError: OutputDocumentFor expects a sequence of Models

代码4:

!pip install imgkit
!apt-get install xvfb
!apt-get install wkhtmltopdf

import imgkit

imgkit.from_file('osm1.html', 'out.jpg')

错误4:

 OSError: wkhtmltoimage exited with non-zero code 1. error:
    QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
    qt.qpa.screen: QXcbConnection: Could not connect to display 
    Could not connect to any X display.
    
    
    You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.

上述任何一种解决方案都可以轻松解决?我不知道更多的方法


Tags: installto代码importurlgetpngusr

热门问题