好的,我可以帮助您编写一个 Python 脚本来批量处理指定文件夹下的所有 .md 文件。为了方便将来的修改和扩展,我们会将各个处理步骤封装成独立的函数,并使用模块化的编程方式。
以下是实现您的需求的代码:
import os
import re
def remove_spaces_in_chinese_sentences(text):
"""
以句号为分界,去掉每一句中文语句中的空格。
"""
# 使用正则表达式匹配中文句子
sentences = re.split('(?<=。)', text)
processed_sentences = []
for sentence in sentences:
# 仅处理包含中文的句子
if re.search('[\u4e00-\u9fff]', sentence):
# 去掉空格
sentence = sentence.replace(' ', '')
processed_sentences.append(sentence)
return ''.join(processed_sentences)
def remove_specified_symbols(text, symbols_to_remove):
"""
去掉指定符号。
"""
for symbol in symbols_to_remove:
text = text.replace(symbol, '')
return text
def convert_latex_to_plain_text(text):
"""
将 LaTeX 格式的公式转为正常文本形式来显示。
"""
# 简单地移除 LaTeX 公式的定界符,如 $...$ 或 $$...$$
# 您可以根据需要使用更复杂的解析器
text = re.sub(r'\${2}(.*?)\${2}', r'\1', text) # 去掉 $$...$$
text = re.sub(r'\$(.*?)\$', r'\1', text) # 去掉 $...$
return text
def replace_images_and_tables(text):
"""
将图片和表格替换为文字,如“图片1”,“表格1”。
"""
image_counter = 1
table_counter = 1
def replace_image(match):
nonlocal image_counter
replacement = f"图片{image_counter}"
image_counter += 1
return replacement
def replace_table(match):
nonlocal table_counter
replacement = f"表格{table_counter}"
table_counter += 1
return replacement
# 替换图片语法 
text = re.sub(r'!\[.*?\]\(.*?\)', replace_image, text)
# 替换表格(这里假设表格使用了 Markdown 表格语法)
text = re.sub(r'(\|.*?\|(\n|$))+', replace_table, text)
return text
def process_file(file_path, symbols_to_remove):
"""
处理单个文件。
"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 执行各个处理步骤
content = remove_spaces_in_chinese_sentences(content)
content = remove_specified_symbols(content, symbols_to_remove)
content = convert_latex_to_plain_text(content)
content = replace_images_and_tables(content)
# 将处理后的内容写回文件,或另存为新文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
def process_markdown_files(folder_path, symbols_to_remove):
"""
处理指定文件夹下的所有 .md 文件。
"""
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.md'):
file_path = os.path.join(root, filename)
process_file(file_path, symbols_to_remove)
print(f"已处理文件: {file_path}")
if __name__ == "__main__":
# 指定要处理的文件夹路径
folder_path = 'path/to/your/folder' # 请替换为实际路径
# 指定要去掉的符号列表
symbols_to_remove = ['符号1', '符号2', '符号3'] # 请替换为实际符号
process_markdown_files(folder_path, symbols_to_remove)
请注意,在上述代码中:
- 模块化设计:每个处理步骤都封装成了独立的函数,便于单独修改和测试。
- 可扩展性:您可以根据需要增加新的处理函数,或修改现有函数的实现。
- 符号移除:在
remove_specified_symbols 函数中,您可以在 symbols_to_remove 列表中添加需要去掉的符号。
- LaTeX 公式转换:
convert_latex_to_plain_text 函数中使用了正则表达式简单地去掉了 LaTeX 公式的定界符,如果需要更复杂的转换,可以使用 sympy 或 latex2mathml 等库。
- 图片和表格替换:
replace_images_and_tables 函数中使用了正则表达式识别 Markdown 中的图片和表格语法,然后替换为指定的文字,并带有计数功能。
使用方法:
- 将上述代码保存为一个 Python 脚本,例如
process_md_files.py。
- 替换脚本中
folder_path 为您要处理的 Markdown 文件夹路径。
- 在
symbols_to_remove 列表中添加您需要移除的符号。
- 运行脚本:
python process_md_files.py。
注意事项:
- 备份文件:运行脚本前,建议备份原始文件,防止意外数据丢失。
- 编码问题:确保读取和写入文件时使用了正确的编码(本例中为
utf-8)。
- 正则表达式:如果文件内容的格式比较复杂,可能需要根据实际情况调整正则表达式。
希望这个脚本能满足您的需求。如有任何问题或需要进一步的功能,请告诉我!