Python之html与markdown互相转换
Python之html与markdown互相转换
- 前言
- 1. html2text
- 2. html2markdown
- 3. pandoc
- 4. 批处理
前言
Typora可以很容易的将md导出为html,我一直都有想法就是将html还原为markdown,于是在网上整理了几种方法,以便后期使用。
如果你只是转换单个文件,推荐直接在线转换:Link Link Link
1. html2text
pip install html2text
转换代码:
import html2textmd_text = open('ret.html', 'r', encoding='utf-8').read()markdown = html2text.html2text(md_text)with open('make2.md', 'w', encoding='utf-8') as file:file.write(markdown)
2. html2markdown
pip install html2markdown
转换代码:
import html2markdownmd_text = open('ret.html', 'r', encoding='utf-8').read()markdown = html2markdown.convert(md_text)with open('make3.md', 'w', encoding='utf-8') as file:file.write(markdown)
经过测试觉得html2text
模块的转换还可以!
3. pandoc
pip install pandoc
在需要转换的目录下打开cmd
将md转换为HTML:
pandoc -f markdown -t html -o a.html a.md
HTML转化为md:
pandoc -f html -t markdown -o b.md b.html
4. 批处理
多个文件同时转换,示例代码:
html转md:
import ospath = r'文件路径'
all = os.listdir(path)
for file in all:if file.endswith('.html'):name = os.path.splitext(file)[0]os.system('cd {} && pandoc -f html -t markdown -o {}.md {}.html '.format(path, os.path.splitext(name)[0], os.path.splitext(name)[0]))
"""
@Author: ZS
@CSDN : https://zsyll.blog.csdn.net/
@Time : 2021/11/25 12:36
"""
import html2text
import osfor root, dirs, files in os.walk(r'E:\Python资料', topdown=True):for name in files:path = os.path.join(root, name)if path.endswith('.html'):with open(path, encoding='utf-8') as html, open(os.path.join(root, os.path.splitext(name)[0] + '.md'), 'w', encoding='utf-8') as md:markdown = html2text.html2text(html.read())md.write(markdown)print(name + ' 转换成功!')
参考:Llink Link
加油!
感谢!
努力!
Python之html与markdown互相转换相关推荐
- 将py文件转换成html,(2条消息)自制 Python小工具 将markdown文件转换成Html文件
今天看到了一个Python库,名为markdown.瞬间就给了我一个灵感,那就是制作一个将markdown文件转换成html文件的小工具. 我的实验环境操作系统: Windows 7 64位 旗舰版 ...
- 使用Python,OpenCV进行图像平移转换
使用Python,OpenCV进行图像平移转换 1. 效果图 2. 原理 3. 源码 参考 这篇博客将介绍如何使用Python,OpenCV对图像进行平移转换.平移是图像沿x轴和y轴的移动.使用平移, ...
- python中list与array互相转换
python中list与array互相转换 author@jason_ql http://blog.csdn.net/lql0716 u = array([[1,2],[3,4]]) m = u.t ...
- python与excel的关系-使用python对excle和json互相转换的示例
python 版本:2.7 只是读取excel的话可以直接使用xlrd 1.excle to json 代码如下 # -*-coding:utf8 -*- import xlrd from colle ...
- Python基本语法元素之温度转换
Python基本语法元素之温度转换 仅凭个人理解自学的笔记 温度转换1.0 温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fahrenheit). ...
- boost::python模块显示重载和自动转换问题的示例
boost::python模块显示重载和自动转换问题的示例 实现功能 C++实现代码 实现功能 boost::python模块显示重载和自动转换问题的示例 C++实现代码 #include <b ...
- vue中将md转成html,Vue/Vue中Html和Markdown互相转换/README.md · 倚栏听风/LearningNotes - Gitee.com...
# Vue中Html和Markdown互相转换 ## 前言 最近想实现的一个功能,就是将系统中的博客进行导出成Markdown格式,后面经过了调研发现有两种方法能够满足需求,一个是Java后台中将HT ...
- blue html中转换,Vue/Vue中Html和Markdown互相转换/README.md · bluemoon/LearningNotes - Gitee.com...
# Vue中Html和Markdown互相转换 ## 前言 最近想实现的一个功能,就是将系统中的博客进行导出成Markdown格式,后面经过了调研发现有两种方法能够满足需求,一个是Java后台中将HT ...
- csv格式清洗与转换python_实例详解Python中 CSV格式清洗与转换
Python中 CSV格式清洗与转换的实例代码 题目: CSV格式清洗与转换 描述 附件是一个CSV格式文件,提取数据进行如下格式转换: ...
最新文章
- QT的滚动条极简教程
- 【转】关于编译链接——gcc/g++
- 苹果大战FBI将加速科技圈的加密技术发展?
- 元宇宙iwemeta: 苹果Apple推动元宇宙产业的生态体系
- 详解MySQL字符串函数CONCAT()、CONCAT_WS()、INSERT()、REPLACE()、LOWER()、UPPER()用法(二)
- php探针源码,服务器探针 (刘海探针)—开源PHP探针
- C++为什么摒弃auto_ptr
- Leetcode 147.对链表进行排序
- html5 服务器手机编程,html5实现服务器发送事件
- c语言中const的作用面试,C语言const关键字作用
- vue+node+mongodb实现的功能
- 【BZOJ】3436: 小K的农场
- 基于深度学习的人脸表情识别实现
- NAND FLASH大页和小页
- python分析qq聊天记录
- HTML渐变背景不重复,CSS背景渐变重复问题
- 现代天线设计——学习笔记(一)
- (PDC2008)Anders Hejlsberg: The Future of C#
- Java 输入汉字姓名 输出 姓名拼音 首字母缩写组合
- R语言统计分析微生物组数据(第三章3)
热门文章
- 1.SOC CPU 2.高通 海思 瑞芯微 联发科 3. LINUX
- nowcoder_B_114514_打表找规律
- 七夕表白如何万无一失!用我这个方法保证你一定成功!
- 解决Python使用pip时提示:WARNING: Ignoring invalid distribution -ywin32等信息
- 44.(leaflet篇)leaflet暗色系地图样式地图(获取滤镜值)
- JAVA计算机毕业设计儿童成长记录系统Mybatis+系统+数据库+调试部署
- MetaIn中自定义poi点在智慧城市中的运用
- 外卖小哥、女装大佬、水泥匠工人,市井中的黑客群像
- java毕业设计演出购票系统Mybatis+系统+数据库+调试部署
- 解决CDH6.2中的kafka消费不到数据问题