webmaster-kazu.com

テクノロジー・データ分析・実践ログ・その他

【Python】HTML/CSS 文字列、ファイル、URL から画像を生成する「html2image」のインストール

カテゴリ: Python【初歩から現場実務的なもの】

HTML/CSS 文字列、ファイル、URL から画像を生成するライブラリ「html2image」のインストールについて解説しています。

「html2image(https://github.com/vgalin/html2image)」は、既存の Web ブラウザーのヘッドレス モードのラッパーとして機能し、URL,HTML+CSS 文字列,ファイルから画像を生成することができます。なお、Google Chrome (Windows, MacOS)、Chromium Browser (Linux)、Microsoft Edgeのうち1つのWebブラウザがインストールされている必要があります。

■Python

今回のPythonのバージョンは、「3.9.9」を使用しています。(Windows11)(pythonランチャーでの確認)

■html2imageをインストールする

html2imageをインストールを行いますが、今回はpipを経由してインストールを行うので、まずWindowsのコマンドプロンプトを起動します。

pip install html2image

起動後、上記のコマンドを入力し、Enterキーを押します。

なお、今回は、pythonランチャーを使用しており、Python Version 3.9.9にインストールを行うために、バージョンの切り替えを行います。

py -3.9 -m pip install html2image

切り替えるために、上記のコマンドを入力し、Enterキーを押します。

Defaulting to user installation because normal site-packages is not writeable

Collecting html2image

Using cached html2image-2.0.4.3-py3-none-any.whl (27 kB)

Collecting websocket-client<2.0.0,>=1.0.0

Using cached websocket_client-1.6.1-py3-none-any.whl (56 kB)

Collecting requests

Using cached requests-2.31.0-py3-none-any.whl (62 kB)

Collecting certifi>=2017.4.17

Using cached certifi-2023.7.22-py3-none-any.whl (158 kB)

Collecting urllib3<3,>=1.21.1

Using cached urllib3-2.0.4-py3-none-any.whl (123 kB)

Collecting idna<4,>=2.5

Using cached idna-3.4-py3-none-any.whl (61 kB)

Collecting charset-normalizer<4,>=2

Using cached charset_normalizer-3.2.0-cp39-cp39-win_amd64.whl (96 kB)

Installing collected packages: urllib3, idna, charset-normalizer, certifi, websocket-client, requests, html2image

WARNING: The script normalizer.exe is installed in 'C:\Users\user_\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.

Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

WARNING: The script wsdump.exe is installed in 'C:\Users\user_\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.

Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

WARNING: The scripts hti.exe and html2image.exe are installed in 'C:\Users\user_\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.

Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Successfully installed certifi-2023.7.22 charset-normalizer-3.2.0 html2image-2.0.4.3 idna-3.4 requests-2.31.0 urllib3-2.0.4 websocket-client-1.6.1

WARNING: You are using pip version 21.2.4; however, version 23.2.1 is available.

You should consider upgrading via the 'C:\Program Files\Python39\python.exe -m pip install --upgrade pip' command.

Enterキーを押すと、インストールが開始され、上記のように「Successfully installed」と表示されます。これが表示されれば、html2imageが正常にインストールされたことになりますが、「WARNING」と表示されました。「WARNING」はエラーではなく警告で、インストールは行われているので、今回は一旦無視しています。

なお、今回はhtml2imageのバージョン2.0.4.3をインストールしました。