Files
python_env/format_text/readbook/formatter_note.py
2026-01-12 09:47:46 +08:00

61 lines
1.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
def format_md_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 去空格
content = content.replace(' ', '')
content = content.replace(' ', '') # 去空格
# 去#
content = content.replace('# ', '')
content = content.replace('#', '')
# 将!转为。
content = content.replace('', '')
content = content.replace('!', '')
# 将;转为。
content = content.replace(';', '')
# 将;转为。
content = content.replace('', '')
# 将.转为句号
content = content.replace('.', '')
# 将(转为(
content = content.replace('', '(')
# 将)转为)
content = content.replace('', ')')
# 将?转为?
content = content.replace('?', '')
# 将,转为,
content = content.replace(',', '')
# 将:转为:
content = content.replace(':', '')
# 让句号后的文案换行
content = content.replace('', '\n')
# 让?后的文案换行
content = content.replace('', '\n')
# 去除空白行
lines = content.splitlines()
non_empty_lines = [line for line in lines if line.strip()]
content = '\n'.join(non_empty_lines)
# 让---后的文案换行
content = content.replace('---', '')
# 将 日星期 转为:日 星期
content = content.replace('日星期', '日 星期')
# 在第二行插入空白行
lines = content.splitlines()
if len(lines) > 0:
lines.insert(1, '')
content = '\n'.join(lines)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
if __name__ == "__main__":
current_dir = os.path.dirname(os.path.abspath(__file__))
print(current_dir)
for root, dirs, files in os.walk(current_dir):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
format_md_file(file_path)