使用Python開(kāi)發(fā)Markdown兼容公式格式轉(zhuǎn)換工具
一、工具背景
在技術(shù)寫(xiě)作中經(jīng)常遇到公式格式問(wèn)題:MathML無(wú)法顯示、LaTeX格式錯(cuò)亂…
本工具實(shí)現(xiàn)以下核心功能:
- 自動(dòng)轉(zhuǎn)換MathML到KaTeX
- 標(biāo)準(zhǔn)化LaTeX公式格式
- 保留原文其他內(nèi)容
- 圖形化操作界面
工具效果演示
二、環(huán)境配置(Windows 10/11)
1. 創(chuàng)建conda環(huán)境
# 打開(kāi)PowerShell執(zhí)行 conda create -n formula_tool python=3.8 conda activate formula_tool pip install tk lxml pyinstaller
2. 獲取XSLT轉(zhuǎn)換文件
# 下載MathML轉(zhuǎn)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 os class FormulaConverterGUI: def __init__(self, master): self.master = master master.title("公式格式轉(zhuǎn)換工具 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="輸入內(nèi)容:") 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="轉(zhuǎn)換公式", command=self.convert) self.convert_btn.pack(side=tk.LEFT, padx=5) self.clear_btn = tk.Button(self.button_frame, text="清空內(nèi)容", command=self.clear) self.clear_btn.pack(side=tk.LEFT, padx=5) self.save_btn = tk.Button(self.button_frame, text="保存結(jié)果", command=self.save_file) self.save_btn.pack(side=tk.LEFT, padx=5) # 輸出框 self.output_label = tk.Label(self.master, text="轉(zhuǎn)換結(jié)果:") 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) # 狀態(tài)欄 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="轉(zhuǎn)換完成") 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) } # 處理塊級(jí)公式 content = patterns['latex_block'].sub( lambda m: f'$$\n{m.group(1).strip()}\n$$', content ) # 處理行內(nèi)公式 content = patterns['latex_inline'].sub( lambda m: f'${m.group(1).strip()}$', content ) # 處理MathML mathml_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"轉(zhuǎn)換失?。簕str(e)}") return content def 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轉(zhuǎn)換原理
def mathml_to_tex(mathml_str): xslt = etree.parse("mathml2tex.xsl") transform = etree.XSLT(xslt) return str(transform(etree.fromstring(mathml_str)))
2. 正則匹配引擎
# 塊級(jí)公式匹配 re.compile(r'\$\$(.*?)\$\$', re.DOTALL) # 行內(nèi)公式匹配 re.compile(r'\$(.*?)\$')
五、工具使用說(shuō)明
1. 界面操作流程
粘貼或輸入包含公式的內(nèi)容
點(diǎn)擊"轉(zhuǎn)換公式"按鈕
查看右側(cè)轉(zhuǎn)換結(jié)果
使用"保存結(jié)果"導(dǎo)出Markdown文件
2. 支持格式示例
原始格式 | 轉(zhuǎn)換后格式 |
---|---|
<math>...</math> | $$ x = \frac{-b}{2a} $$ |
\Gamma(z)... | $$\Gamma(z)...$$ |
$E=mc^2$ | $E=mc^2$ |
到此這篇關(guān)于使用Python開(kāi)發(fā)Markdown兼容公式格式轉(zhuǎn)換工具的文章就介紹到這了,更多相關(guān)Python Markdown格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何用Python實(shí)現(xiàn)簡(jiǎn)單的Markdown轉(zhuǎn)換器
- Python實(shí)現(xiàn)從Markdown到PDF的轉(zhuǎn)換的方法
- 使用Python構(gòu)建Markdown轉(zhuǎn)Word文檔轉(zhuǎn)換器
- 利用Python開(kāi)發(fā)Markdown表格結(jié)構(gòu)轉(zhuǎn)換為Excel工具
- 使用Python將Markdown格式轉(zhuǎn)為EPUB電子書(shū)格式的代碼實(shí)現(xiàn)
- 基于Python實(shí)現(xiàn)Markdown轉(zhuǎn)ePub
- 使用Python將Markdown文件轉(zhuǎn)換為Word的三種方法
相關(guān)文章
使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL的方法
在日常工作中,我們經(jīng)常會(huì)遇到需要處理大量文件并將數(shù)據(jù)存儲(chǔ)至數(shù)據(jù)庫(kù)或整合到一個(gè)文件的需求,本文將向大家展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL數(shù)據(jù)庫(kù)中,需要的朋友可以參考下2024-01-01實(shí)例講解Python的函數(shù)閉包使用中應(yīng)注意的問(wèn)題
這里我們來(lái)以實(shí)例講解Python的函數(shù)閉包使用中應(yīng)注意的問(wèn)題,主要針對(duì)閉包后新生成的變量來(lái)不及初始化而導(dǎo)致找不到變量的錯(cuò)誤出現(xiàn),需要的朋友可以參考下2016-06-06Python使用paddleOCR批量識(shí)別pdf的方法
PaddleOCR可以在圖像、文本、表格等多種場(chǎng)景下進(jìn)行文字識(shí)別,本文主要介紹了Python使用paddleOCR批量識(shí)別pdf的方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Python灰度變換中的對(duì)數(shù)變換專(zhuān)項(xiàng)分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變?cè)磮D像中每個(gè)像素灰度值的方法。目的是改善畫(huà)質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10Python常用的json標(biāo)準(zhǔn)庫(kù)
今天小編就為大家分享一篇關(guān)于Python常用的json標(biāo)準(zhǔn)庫(kù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02python切片復(fù)制列表的知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python切片復(fù)制列表的知識(shí)點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10python3 動(dòng)態(tài)模塊導(dǎo)入與全局變量使用實(shí)例
今天小編就為大家分享一篇python3 動(dòng)態(tài)模塊導(dǎo)入與全局變量使用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12