一、安装依赖

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. python报错:Pip 20.3+ break proxy connection

    参考: https://www.cnblogs.com/devilmaycry812839668/p/17872452.html =================================== ...

  2. 【转载】 Ubuntu下使用VSCode的launch.json及tasks.json编写

    版权声明:本文为CSDN博主「子木呀」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_41687938/a ...

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

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

  4. WPF Button自定义样式收集 带有图片的Button

    此篇只是收集平时写过的样式~ 带有图片的Button 为Button设定了一些附加属性,用于添加图片到Button. 比如初始化图片和点击后的图片 public static readonly Dep ...

  5. 如何切换git的HEAD分支

    git remote set-head origin some_branch  

  6. OpenHarmony编译构建系统详解,从零搭建windows下开发环境,巨方便!

    自从OpenHarmony更新了dev-tool,就可以在windows下构建鸿蒙(轻量型)系统了,这对于进行MCU开发的朋友们,学习鸿蒙OS会友好许多!我们可以更快的构建出系统,方便快速学习和验证. ...

  7. win10缺少SNMP服务解决办法

    一,以管理员的身份启动Powershell 第一步在win10系统任务栏上,点击搜索图标,输入"PowerShell",如下图所示: 2 第二步搜索到PowerShell之后,鼠标 ...

  8. Modbus ASCII 获取数据

    根据银河高低温试验箱协议读取数据 1.协议内容 8.1:通讯协议介绍 8.1.5 通讯设置 本通讯协议使用异步串行通讯方式,1 个起始位.8 个数据位.2 个停止 位.无奇偶校验数据通讯格式,其中数据 ...

  9. Coursera, Big Data 5, Graph Analytics for Big Data, Week 3

    Graph Analytics 有哪些类型 node type (labels) node schema: attributes 组成了schema. 同样的, Edge也有 Edge Type 和E ...

  10. Cloudflare D1 - 免费数据存储

    前言 自从上次将博客项目的图片从 七牛云 迁到了 Cloudflare R2 之后就发现,Cloudflare 这个赛博菩萨的产品是真的不错,非常的适合白嫖,DevNow 项目作为一个开源博客,整体来 ...