Pythonを使用しマークダウンをHTMLに変換する

スポンサーリンク

Pythonを使用しマークダウンをHTMLに変換してみます。

■Python

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

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
instagram-private-api-extensions 0.3.8 requires moviepy==0.2.3.2, but you have moviepy 1.0.3 which is incompatible.
instabot 0.117.0 requires requests>=2.22.0, but you have requests 2.21.0 which is incompatible.
instabot 0.117.0 requires six>=1.14.0, but you have six 1.12.0 which is incompatible.
instabot 0.117.0 requires urllib3>=1.25.7, but you have urllib3 1.24.3 which is incompatible

バージョン「3.8.5」だと、上記のエラーが発生したので、「3.6.8」でインストールを行う。「gh-md-to-html」を使用するには、Python3.6が良い。

■サードパーティ製のライブラリ「gh-md-to-html」を使用する

今回、Pythonを使用しマークダウンをHTMLに変換を行いますが、変換する際にサードパーティ製のライブラリ「gh-md-to-html」(https://github.com/phseiff/github-flavored-markdown-to-html)を使用します。

■gh-md-to-htmlをインストールする

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

pip install gh-md-to-html

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

なお、今回は、pythonランチャーを使用しており、Python Version 3.6.8にインストールを行うために、pipを使う場合にはコマンドでの切り替えを行います。

py -3.6 -m pip install gh-md-to-html

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

Collecting gh-md-to-html
  Downloading https://files.pythonhosted.org/packages/f6/40/d56acfcdd20d2e7195701a1b9e52d414ad3fdf57a4aaa935769346dae9ef/gh_md_to_html-1.11.10-py3-none-any.whl (59kB)
    100% |████████████████████████████████| 61kB 952kB/s
Collecting beautifulsoup4 (from gh-md-to-html)
  Using cached https://files.pythonhosted.org/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl
Requirement already satisfied: requests in c:\users\user\appdata\roaming\python\python36\site-packages (from gh-md-to-html) (2.21.0)
Collecting shellescape (from gh-md-to-html)
  Downloading https://files.pythonhosted.org/packages/d0/f4/0081137fceff5779cd4205c1e96657e41cc2d2d56c940dc8eeb6111780f7/shellescape-3.8.1-py2.py3-none-any.whl
Collecting Pillow>=8.0.1 (from gh-md-to-html)
  Downloading https://files.pythonhosted.org/packages/cf/f7/124c119cf1d4a95dd15913038aba427ab30b7220ec47d2c5617cf56db7cb/Pillow-8.2.0-cp36-cp36m-win_amd64.whl (2.2MB)
    100% |████████████████████████████████| 2.2MB 2.6MB/s
Collecting webcolors (from gh-md-to-html)
  Downloading https://files.pythonhosted.org/packages/12/05/3350559de9714b202e443a9e6312937341bd5f79f4e4f625744295e7dd17/webcolors-1.11.1-py3-none-any.whl
Collecting soupsieve>1.2; python_version >= "3.0" (from beautifulsoup4->gh-md-to-html)
  Downloading https://files.pythonhosted.org/packages/36/69/d82d04022f02733bf9a72bc3b96332d360c0c5307096d76f6bb7489f7e57/soupsieve-2.2.1-py3-none-any.whl
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (2020.12.5)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (1.24.3)
Installing collected packages: soupsieve, beautifulsoup4, shellescape, Pillow, webcolors, gh-md-to-html
Could not install packages due to an EnvironmentError: [WinError 5] アクセスが拒否されました。: 'C:\\Program Files\\Python36\\Lib\\site-packages\\soupsieve'
Consider using the `--user` option or check the permissions.

Enterキーを押すと、インストールが開始され、「Could not install packages due to an EnvironmentError: [WinError 5] アクセスが拒否されました。」というエラーが表示されました。

py -3.6 -m pip install gh-md-to-html --user

エラーの問題を解決するために、上記のコマンドを入力し、Enterキーを押す。

Collecting gh-md-to-html
Using cached https://files.pythonhosted.org/packages/f6/40/d56acfcdd20d2e7195701a1b9e52d414ad3fdf57a4aaa935769346dae9ef/gh_md_to_html-1.11.10-py3-none-any.whl
Collecting webcolors (from gh-md-to-html)
Using cached https://files.pythonhosted.org/packages/12/05/3350559de9714b202e443a9e6312937341bd5f79f4e4f625744295e7dd17/webcolors-1.11.1-py3-none-any.whl
Collecting beautifulsoup4 (from gh-md-to-html)
Using cached https://files.pythonhosted.org/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl
Collecting shellescape (from gh-md-to-html)
Using cached https://files.pythonhosted.org/packages/d0/f4/0081137fceff5779cd4205c1e96657e41cc2d2d56c940dc8eeb6111780f7/shellescape-3.8.1-py2.py3-none-any.whl
Requirement already satisfied: requests in c:\users\user\appdata\roaming\python\python36\site-packages (from gh-md-to-html) (2.21.0)
Collecting Pillow>=8.0.1 (from gh-md-to-html)
Using cached https://files.pythonhosted.org/packages/cf/f7/124c119cf1d4a95dd15913038aba427ab30b7220ec47d2c5617cf56db7cb/Pillow-8.2.0-cp36-cp36m-win_amd64.whl
Collecting soupsieve>1.2; python_version >= "3.0" (from beautifulsoup4->gh-md-to-html)
Using cached https://files.pythonhosted.org/packages/36/69/d82d04022f02733bf9a72bc3b96332d360c0c5307096d76f6bb7489f7e57/soupsieve-2.2.1-py3-none-any.whl
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (2020.12.5)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\user\appdata\roaming\python\python36\site-packages (from requests->gh-md-to-html) (3.0.4)
Installing collected packages: webcolors, soupsieve, beautifulsoup4, shellescape, Pillow, gh-md-to-html
Successfully installed Pillow-8.2.0 beautifulsoup4-4.9.3 gh-md-to-html-1.11.10 shellescape-3.8.1 soupsieve-2.2.1 webcolors-1.11.1

Enterキーを押すと、インストールが開始され、「Successfully installed」と表示されました。エラーは表示されなかったので、正常にインストールが完了する。

■マークダウンをHTMLに変換する

完了後、gh-md-to-htmlを使用しマークダウンをHTMLに変換するスクリプトを書いていきます。

■コード

import gh_md_to_html

html = gh_md_to_html.main(r'C:\Users\user\test\test.txt')

print(html)

インポートでgh-md-to-htmlモジュールを呼び出します。その後にhtmlという変数を作成し、その中でgh_md_to_html.main()関数を使用します。括弧内にはマークダウンファイルを指定します。指定後、格納。

格納後、HTMLに変換されたマークダウンをprint関数で出力します。

なお、今回は「test.txt」というテキストファイル内に上記のコードを記述し、保存しています。フォルダパスは「C:\Users\user\test」です。ちなみにファイルは、.mdファイルも使用することができます。

■実行

このスクリプトを「markdown_conversion.py」という名前で保存し、コマンドプロンプトから実行してみます。

実行してみると、今回指定したマークダウンファイル(テキストファイル)がHTMLに変換されて出力されることを確認できました。

コメント

タイトルとURLをコピーしました