Hongmu Notes
Home Language Notes Convert Markdown text to HTML using Python.
Language Notes python

Convert Markdown text to HTML using Python.

Convert Markdown text to HTML using Python.

We can use Python's Markdown library to convert Markdown text files into HTML files.

Specify the input directory (containing.md files) and the output directory (containing.html files) in your code. Below is a template Python script.

import os
import markdown

# 指定输入和输出目录
input_dir = 'text'
output_dir = 'html_text'

# 序列化并替换.md到.html
def convert_md_to_html(input_file_name, output_file_name):
    input_file_path = os.path.join(input_dir, input_file_name)
    output_file_path = os.path.join(output_dir, output_file_name)
    
    # 读取输入文件
    with open(input_file_path, 'r', encoding='utf-8') as input_file:
        text = input_file.read()
        
        # 将Markdown文本转换为HTML
        html = markdown.markdown(text)
        
        # 将结果写入输出文件
        with open(output_file_path, 'w', encoding='utf-8') as output_file:
            output_file.write(html)
            
# 遍历输入目录,并处理每个.markdown文件
for f in os.listdir(input_dir):
    if f.endswith('.md'):
        input_file_name = f
        output_file_name = f.replace('.md', '.html')
        convert_md_to_html(input_file_name, output_file_name)

This code converts a Markdown text file into an HTML file and saves the resulting file in the `html_text` directory.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Import " requests" could not be resolved from source

Import " requests" could not be resolved from source Language Notes python

This error occurred when using Pylance, a Python language server. Among them, " Import " requests" could not be resolved from source" This error message indicates that the requests module is referenced in your code, but Pylance cannot find the location of the module from the source. The possible reason is that you don't have an …
👁 1771
Reinstall Python and requests libraries

Reinstall Python and requests libraries Language Notes python

If you meet " Import " requests" could not be resolved from source" You can try reinstalling Python and requests libraries. First, you need to uninstall the current Python and requests libraries. On Windows systems, you can use the command prompt …
👁 439
WARNING: Skipping requests as it is not installed.

WARNING: Skipping requests as it is not installed. Language Notes python

This error message indicates that your Python environment lacks the requests library, and this library is called in your code, so Python cannot execute your code normally. The solution to this problem is to install the requests library. You can use the pip command on the command line to install: pip install requests If you are using Python3, you can use the pip3 command …
👁 2100
Python reported an error. What is the reason? AttributeError: partially initialized module ' openai'  has no attribute ' Completion' (most likely due to a circular import)

Python reported an error. What is the reason? AttributeError: partially initialized module ' openai' has no attribute ' Completion' (most likely due to a circular import) Language Notes python

This error prompt indicates that there is a problem when importing the openai module and the specific Completion attribute, which is probably caused by circular import. The reason for this error may be that two or more modules in your code are imported from each other, forming a circular dependency. For example, module A imports module B, and module B imports module A.. Cyclic import will cause the module not to initialize correctly when loading, thus …
👁 656
How does python package and run on Windows?

How does python package and run on Windows? Language Notes python

The most common way to package and run Python programs on Windows is to use PyInstaller. PyInstaller is a free and cross-platform Python application packaging tool, which can package Python code and its dependent libraries into an independent executable file, making it possible to run Python programs on systems without Python interpreters installed. The following is to make …
👁 300
What programming software is good for python development?

What programming software is good for python development? Language Notes python

There are many programming softwares developed by Python. The following are some commonly used Python integrated development environments (IDE) and editors: PyCharmPyCharm is an integrated development environment developed by JetBrains, which is specially designed for Python developers. PyCharm is powerful and provides a series of advanced functions such as code completion, debugging, version control, code refactoring, etc., which can help developers improve their work …
👁 175

Recommended reading

Responsive kitchenware website template – 0899

Responsive kitchenware website template – 0899 Practical Collection Yiyou template

An EyouCMS responsive website template designed for the kitchenware industry. Its modern and practical design is ideal for showcasing kitchen products, kitchen accessories, brand stories, and spatial arrangements. This template helps kitchenware brands showcase their products online and attract both household and commercial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 57
Responsive HR Service Website Template 0101

Responsive HR Service Website Template 0101 Practical Collection Yiyou template

An eyouCMS responsive website template designed specifically for HR service providers. Its professional and sophisticated design effectively showcases HR services, recruitment solutions, and success stories, helping HR agencies attract corporate clients and expand their business online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eyouCMS | eyouCMS...
👁 65
(Adaptive Mobile Version) Responsive Pink Beauty and Plastic Surgery Center – Cosmetics Industry Enterprise Website Template Source Code Download – 0501

(Adaptive Mobile Version) Responsive Pink Beauty and Plastic Surgery Center – Cosmetics Industry Enterprise Website Template Source Code Download – 0501 Practical Collection pbootcms Template

A responsive pink website template designed for beauty and plastic surgery clinics as well as cosmetic product companies, compatible with both PC and WAP devices. Its sophisticated and elegant design makes it ideal for showcasing services and products offered by beauty and plastic surgery hospitals or cosmetic brands. This template helps medical aesthetics and beauty brands attract female consumers online. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 34
PBOOTCMS Authorization Code Record

PBOOTCMS Authorization Code Record Practical Collection pb tool

Some time ago, a friend gifted me a mobile device authorization code – reportedly at a considerable cost – without me asking why. Recently, I needed this code again, so I found it and recorded it once more! I have no intention of selling it; it's simply for my own convenience when looking it up. If you're interested, please visit the official website. 🔒 Hidden content: Paid subscription – 'sn' = '3822550667', // Authorized user's phone –' sn_user' =...
👁 200
Home Furnishings & Sofas Website Template 0900

Home Furnishings & Sofas Website Template 0900 Practical Collection Yiyou template

This EyouCMS template is ideal for the home furnishings, furniture, and sofa industries. Its modern and comfortable design style is perfect for showcasing furniture products, sofa collections, home decor combinations, and brand stories. It helps furniture brands present their products online and attract household consumers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 51
Environmental Waste Gas and Wastewater Treatment Project Website Template 0102

Environmental Waste Gas and Wastewater Treatment Project Website Template 0102 Practical Collection Yiyou template

This EyouCMS template is ideal for environmental protection, exhaust gas and wastewater treatment, as well as engineering enterprises. Its eco-friendly and tech-driven design effectively showcases exhaust gas and wastewater treatment technologies, engineering projects, and customized solutions. It helps environmental engineering firms demonstrate their expertise online and attract both industrial and municipal clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 46