[python] html table을 Markdown으로 변경하기.

[python] html table을 Markdown으로 변경하기. 

 

htmltabletomdhtml 테이블을 마크다운으로 변환하기 위한 것입니다. 또한, 테이블 셀 내부의 내용은 HTML이 포함되어 있는 경우 마크다운으로 변환할 수 있으며, 이를 위해 라이브러리를 사용합니다 htmltomarkdown.

 

pip install htmltabletomd

 

https://pypi.org/project/htmltabletomd/

 

htmltabletomd

Convert html table to markdown table

pypi.org

from bs4 import BeautifulSoup
from markdownify import markdownify as md
import os

def convert_html_table_to_md(html_file_path, output_md_file_path):
    # HTML 파일 읽기
    with open(html_file_path, 'r', encoding='utf-8') as file:
        html_content = file.read()

    # BeautifulSoup을 사용하여 HTML 파싱
    soup = BeautifulSoup(html_content, 'html.parser')

    # HTML을 Markdown으로 변환
    markdown_content = md(str(soup))

    # 결과를 Markdown 파일로 저장
    with open(output_md_file_path, 'w', encoding='utf-8') as file:
        file.write(markdown_content)
    
    print(f"Converted HTML table to Markdown and saved as: {output_md_file_path}")

# 사용 예시
#html_file_path = 'path_to_your_html_file.html'  # 변환할 HTML 파일 경로
html_file_path = 'test_001.html'  # 변환할 HTML 파일 경로
output_md_file_path = 'output_markdown_file.md'  # 저장할 Markdown 파일 경로

convert_html_table_to_md(html_file_path, output_md_file_path)