基于Python开发Markdown兼容公式格式转换工具
一、工具背景
在技术写作中经常遇到公式格式问题:MathML无法显示、LaTeX格式错乱…
本工具实现以下核心功能:
✅ 自动转换MathML到KaTeX
✅ 标准化LaTeX公式格式
✅ 保留原文其他内容
✅ 图形化操作界面
工具效果演示
二、环境配置(Windows 10/11)
1. 创建conda环境
# 打开PowerShell执行
conda create -n formula_tool python=3.8
conda activate formula_tool
pip install tk lxml pyinstaller
2. 获取XSLT转换文件
# 下载MathML转LaTeX的XSLT文件
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/ronaldo1967/MathML-to-LaTeX/master/mathml2tex.xsl" -OutFile mathml2tex.xsl
三、完整Python代码(带GUI)
# formula_converter_gui.py
import tkinter as tk
from tkinter import scrolledtext, filedialog
import re
from lxml import etree
import osclass FormulaConverterGUI:def __init__(self, master):self.master = mastermaster.title("公式格式转换工具 v1.0")master.geometry("800x600")# 界面组件self.create_widgets()self.xslt_path = "mathml2tex.xsl"def create_widgets(self):# 输入框self.input_label = tk.Label(self.master, text="输入内容:")self.input_label.pack(pady=5)self.input_text = scrolledtext.ScrolledText(self.master, wrap=tk.WORD, height=15)self.input_text.pack(fill=tk.BOTH, expand=True, padx=10)# 操作按钮self.button_frame = tk.Frame(self.master)self.button_frame.pack(pady=10)self.convert_btn = tk.Button(self.button_frame, text="转换公式", command=self.convert)self.convert_btn.pack(side=tk.LEFT, padx=5)self.clear_btn = tk.Button(self.button_frame, text="清空内容", command=self.clear)self.clear_btn.pack(side=tk.LEFT, padx=5)self.save_btn = tk.Button(self.button_frame, text="保存结果", command=self.save_file)self.save_btn.pack(side=tk.LEFT, padx=5)# 输出框self.output_label = tk.Label(self.master, text="转换结果:")self.output_label.pack(pady=5)self.output_text = scrolledtext.ScrolledText(self.master, wrap=tk.WORD, height=15)self.output_text.pack(fill=tk.BOTH, expand=True, padx=10)# 状态栏self.status_bar = tk.Label(self.master, text="就绪", bd=1, relief=tk.SUNKEN, anchor=tk.W)self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)def convert(self):content = self.input_text.get("1.0", tk.END)converted = self.process_content(content)self.output_text.delete("1.0", tk.END)self.output_text.insert(tk.END, converted)self.status_bar.config(text="转换完成")def process_content(self, content):patterns = {'latex_block': re.compile(r'\$\$(.*?)\$\$', re.DOTALL),'latex_inline': re.compile(r'\$(.*?)\$'),'mathml': re.compile(r'<math.*?>(.*?)</math>', re.DOTALL)}# 处理块级公式content = patterns['latex_block'].sub(lambda m: f'$$\n{m.group(1).strip()}\n$$', content)# 处理行内公式content = patterns['latex_inline'].sub(lambda m: f'${m.group(1).strip()}$', content)# 处理MathMLmathml_matches = patterns['mathml'].finditer(content)for match in mathml_matches:try:tex = self.mathml_to_tex(match.group(0))content = content.replace(match.group(0), f'$$ {tex} $$')except Exception as e:self.status_bar.config(text=f"转换失败:{str(e)}")return contentdef mathml_to_tex(self, mathml_str):xslt = etree.parse(self.xslt_path)transform = etree.XSLT(xslt)doc = etree.fromstring(mathml_str)result = transform(doc)return str(result).strip()def clear(self):self.input_text.delete("1.0", tk.END)self.output_text.delete("1.0", tk.END)self.status_bar.config(text="已清空")def save_file(self):file_path = filedialog.asksaveasfilename(defaultextension=".md",filetypes=[("Markdown文件", "*.md"), ("所有文件", "*.*")])if file_path:with open(file_path, 'w', encoding='utf-8') as f:f.write(self.output_text.get("1.0", tk.END))self.status_bar.config(text=f"文件已保存至:{file_path}")if __name__ == "__main__":root = tk.Tk()app = FormulaConverterGUI(root)root.mainloop()
四、核心功能解析
1. MathML转换原理
def mathml_to_tex(mathml_str):xslt = etree.parse("mathml2tex.xsl")transform = etree.XSLT(xslt)return str(transform(etree.fromstring(mathml_str)))
2. 正则匹配引擎
# 块级公式匹配
re.compile(r'\$\$(.*?)\$\$', re.DOTALL)# 行内公式匹配
re.compile(r'\$(.*?)\$')
五、工具使用说明
1. 界面操作流程
- 粘贴或输入包含公式的内容
- 点击"转换公式"按钮
- 查看右侧转换结果
- 使用"保存结果"导出Markdown文件
2. 支持格式示例
原始格式 | 转换后格式 |
---|---|
<math>...</math> | $$ x = \frac{-b}{2a} $$ |
\Gamma(z)... | $$\Gamma(z)...$$ |
$E=mc^2$ | $E=mc^2$ |
六、常见问题解答
Q1: 转换后公式显示异常?
检查是否包含非标准LaTeX语法,建议使用CSDN官方支持的KaTeX语法
Q2: 如何添加新格式支持?
修改process_content
方法,添加新的正则匹配规则