当前位置: 首页 > news >正文

html转md的Python程序

读取目录及子目录下html文件转换为md格式

很多知识库不支持html格式,用本程序可以批量转换为md格式

import os
import html2text
from pathlib import Pathdef convert_html_to_md(html_path, output_dir):with open(html_path, 'r', encoding='utf-8') as f:html_content = f.read()# 使用 html2text 转换为 Markdownh = html2text.HTML2Text()h.ignore_links = False  # 保留链接markdown_content = h.handle(html_content)# 生成输出文件路径output_file = output_dir / (html_path.stem + ".md")with open(output_file, 'w', encoding='utf-8') as f:f.write(markdown_content)print(f"✅ 转换完成: {html_path} -> {output_file}")def batch_convert_all_html(root_path):root_path = Path(root_path).resolve()output_dir = root_path / "python_output"output_dir.mkdir(exist_ok=True)for dirpath, dirnames, filenames in os.walk(root_path):for file in filenames:if file.lower().endswith((".html", ".htm")):html_file_path = Path(dirpath) / fileconvert_html_to_md(html_file_path, output_dir)if __name__ == "__main__":current_directory = Path(__file__).parentbatch_convert_all_html(current_directory)

http://www.xdnf.cn/news/790417.html

相关文章:

  • 图解深度学习 - 激活函数和损失函数
  • 数据安全中心是什么?如何做好数据安全管理?
  • [内核开发手册] ARM汇编指令速查表
  • 【Linux】linux基础指令
  • 用python制作一个消消乐游戏(限时关卡挑战版)
  • 【Linux】进程虚拟地址空间详解
  • 太阳敏感器:卫星姿态控制的“指南针
  • istringstream
  • qt 事件顺序
  • Windows安装PostgreSQL(16.9)
  • 半导体行业-研发设计管理数字化转型案例分享
  • 【Typst】6.布局函数
  • c# 显示正在运行的线程数
  • lsinitramfs命令
  • 新德通科技:以创新驱动光通信一体化发展,赋能全球智能互联
  • Vue3.5 企业级管理系统实战(二十二):动态菜单
  • 代码随想录60期day56
  • 海盗64位GameServer的使用体验
  • 【自动思考记忆系统】demo (Java版)
  • 记一次sql按经纬度计算距离
  • 市面上有真正的静态住宅ip吗?
  • android NDK 的 -> 是什么意思
  • LRC and VIP
  • mac环境下的python、pycharm和pip安装使用
  • C++核心编程_ 函数调用运算符重载
  • PPO: Proximal Policy Optimization Algorithms
  • 全面解析 Windows CE 定制流程:从内核到设备部署
  • 基于MATLAB的FTN调制和硬判决的实现
  • 手把手教你用Appsmith打造企业级低代码平台:从部署到性能调优实战
  • PPO和GRPO算法