一、安装依赖

pip install html2text

代码实现

import os
import shutil
import html2text def convert_html2md(src_html, target_md):
#'''html转md'''
with open(src_html, 'r',encoding='utf-8') as f:
html = f.read()
markdown = html2text.html2text(html)
with open(target_md, 'w',encoding='utf-8') as f:
f.write(markdown) def batch_convert(root_path):
# 将指定目录下的html批量转换为md
for root, dirs, files in os.walk(root_path):
for filename in files:
if filename.endswith('.html'):
file_path = os.path.join(root, filename)
# print(file_path)
target_md = os.path.join(root,filename.replace('.html','.md'))
if os.path.exists(target_md):
print(f'{target_md},文件已存在,不在生成')
continue
convert_html2md(src_html=file_path,target_md=target_md) def batch_cp_md_file(root_path):
# 将转换完成后的md文件复制到单独的目录
for root, dirs, files in os.walk(root_path):
for filename in files:
if filename.endswith('.md'):
file_path = os.path.join(root, filename)
# print(root)
target_path = root if str(root).endswith('-md') \
else f'{root}-md'
# print(target_path)
target_file = os.path.join(target_path, filename)
if os.path.exists(target_file):
print(f"{target_file},已存在跳过复制文件")
continue
if not os.path.exists(target_path):
os.makedirs(target_path)
shutil.copyfile(file_path,target_file) if __name__=='__main__':
root_path = 'd:\\阿里云盘-课程'
batch_convert(root_path=root_path)
batch_cp_md_file(root_path=root_path)

python将html批量转换为md的更多相关文章

  1. python脚本-excel批量转换为csv文件

    pandas和SQL数据分析实战视频教程 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2& ...

  2. Python之文件处理-批量修改md文档内容

    目录 Python之文件处理-批量修改md文档内容 Python之文件处理-批量修改md文档内容 #!/usr/bin/env python # -*- coding:utf-8 -*- import ...

  3. 【百度地图API】如何批量转换为百度经纬度

    原文:[百度地图API]如何批量转换为百度经纬度 摘要: 百度地图API的官网上提供了常用坐标转换的示例.但是,一次只能转换一个,真的非常麻烦!!这里结合了官方的示例,自制一个批量转换工具,供大家参考 ...

  4. 如何将lrc歌词文件批量转换为ANSI编码?

    有些MP3.MP4或学习机只能播放ANSI编码的歌词文件,可是从网站上下载的歌词大多是UTF-8或者其它机器支持不了的编码,如何批量将这些lrc歌词文件转换成ANSI编码的文件呢? 工具/原料 萍客T ...

  5. C# CAD批量转换为图片

    最近写了个工具,将指定目录下的CAD文件批量转换为图片格式. 首先需要添加对应的引用 : 在AutoCAD2008的环境下对应AutoCAD 2008 Type Library 和 AutoCAD/O ...

  6. pyautogui_pdf批量转换为TXT

    pyautogui_pdf批量转换为TXT, 用pdf自带无损转换 # -*- coding: utf-8 -*- """ Created on Thu May 5 15 ...

  7. Python中文语料批量预处理手记

    手记实用系列文章: 1 结巴分词和自然语言处理HanLP处理手记 2 Python中文语料批量预处理手记 3 自然语言处理手记 4 Python中调用自然语言处理工具HanLP手记 5 Python中 ...

  8. 如果把PNG、JPG、BMP和GIF文件批量转换为ICO文件?

    有时候需要将大量的图片文件(比如PNG.JPG.BMP和GIF文件)批量转换为ICO图标文件,如果一个一个操作,非常费时间.本文将介绍如何用Dr. Folder软件快速批量转换图片文件为ICO图标文件 ...

  9. python实现IP地址转换为32位二进制

    python实现IP地址转换为32位二进制 #!/usr/bin/env python # -*- coding:utf-8 -*- class IpAddrConverter(object): de ...

  10. Python 通过文件名批量移动文件

    Python 通过文件名批量移动文件 https://stackoverflow.com/questions/28913088/moving-files-with-wildcards-in-pytho ...

随机推荐

  1. 【转载】 tensorflow batch_normalization的正确使用姿势

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/computerme/article/de ...

  2. Hessian Free Optimization——外国网友分享的“共轭梯度”的推导

    外国网友分享的"共轭梯度"的推导: https://andrew.gibiansky.com/blog/machine-learning/hessian-free-optimiza ...

  3. &times被替换成x 的解决办法

    今天写代码遇到一个很有趣的问题: 在php中使用echo 输出url的时候当url中包含&times字段时就会被html直接解析成 x (乘号)这样一来我返回的地址就不能正常访问url了: 解 ...

  4. udp协议实现组播功能

    /*************************************************************************************************** ...

  5. python调用第三方java包实例

    先看结果: 对于python与java的互调,我一开始是用的py4j,但是后来发现在使用方法的时候,不知道如何在python中导入jar包,然后网上的资料也比较少.后来想不出来办法,又看到有Jpype ...

  6. 2024 年了,IT 运维监控系统都有哪些推荐?

    大浪淘沙,2024 年的今天,市面上很多监控系统慢慢淡出了大家的视野,而一些新的监控系统也逐渐崭露头角.今天我们就来看看 2024 年的当下,哪些 IT 运维监控系统最值得关注. Prometheus ...

  7. 使用Web Component定义自己的专属网页组件

    什么是Web Component Web Component是一套Web浏览器的技术和规范,能够让开发者定制自己的HTML元素 来自MDN的描述: Web Component 是一套不同的技术,允许你 ...

  8. flex数据绑定

    1 .方法绑定    [Bindable(event="myFlagChanged")] private function isEnabled():String { if (myF ...

  9. 9.150 Predefined macros

    9.150 Predefined macros The ARM compiler predefines a number of macros. These macros provide informa ...

  10. element-ui 表格控制列显隐简单方案

    核心是使用v-if控制列的显隐 <template> <div> <div v-for="(item, index) in tables" :key= ...