1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
| import os import xml.etree.ElementTree as ET from docx import Document from docx.oxml.ns import qn from lxml import etree import urllib.parse
def clean_xml(xml_str): try: root = etree.fromstring(xml_str)
cleaned_str = etree.tostring(root, pretty_print=True, encoding='unicode')
return cleaned_str except etree.XMLSyntaxError as e: return f"Error cleaning XML: {e}"
def lxml_parse(xml_str): try: root = etree.fromstring(clean_xml(xml_str)) except Exception as e: print(f'Error: {e}') return xml_str formula_text = convert_omath_to_text(root) return f'${formula_text}$'
def extract_text(elem): if elem.tag.endswith('}t'): return parse_text(elem.text) or '' elif elem.tag.endswith('}d'): return parse_bracket(elem) elif elem.tag.endswith('}sSup'): return parse_ssup(elem) elif elem.tag.endswith('}sSub'): return parse_ssub(elem) elif elem.tag.endswith('}sSubSup'): return parse_ssubsup(elem) elif elem.tag.endswith('}f'): return parse_fraction(elem) elif elem.tag.endswith('}rad'): return parse_radical(elem) else: return ''.join(extract_text(child) for child in elem)
def convert_omath_to_text(omath_elem): return extract_text(omath_elem)
def parse_fraction(elem): numerator = extract_text(elem.find('.//m:num', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) denominator = extract_text(elem.find('.//m:den', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) return f"\\frac{{{numerator}}}{{{denominator}}}"
def parse_ssup(elem): base = extract_text(elem.find('.//m:e', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) exponent = extract_text(elem.find('.//m:sup', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) return f"{base}^{{{exponent}}}"
def parse_ssub(elem): base_elem = elem.find('.//m:e[1]', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'}) subscript_elem = elem.find('.//m:sub[1]', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'}) base = extract_text(base_elem) if base_elem is not None else "" subscript = extract_text(subscript_elem) if subscript_elem is not None else "" return f"{base}_{{{subscript}}}"
def parse_ssubsup(elem): base = extract_text(elem.find('.//m:e', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) exponent = extract_text(elem.find('.//m:sup', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) subscript = extract_text(elem.find('.//m:sub', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) return f"{base}_{{{subscript}}}^{{{exponent}}}"
def parse_bracket(elem): base = extract_text(elem.find('.//m:e', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'})) return f"\\left( {base} \\right)"
def parse_radical(elem): content = extract_text(elem.find('.//m:e', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'}))
degree_elem = elem.find('.//m:deg', namespaces={'m': 'http://schemas.openxmlformats.org/officeDocument/2006/math'}) if degree_elem is not None: degree = extract_text(degree_elem) return f"\\sqrt[{degree}]{{{content}}}" else: return f"\\sqrt{{{content}}}"
def parse_text(text): """ Parse the text and replace common symbol names with LaTeX equivalents. """ symbol_map = { 'α': '\\alpha', 'β': '\\beta', 'γ': '\\gamma', 'δ': '\\delta', 'ε': '\\epsilon', 'ζ': '\\zeta', 'η': '\\eta', 'θ': '\\theta', 'ι': '\\iota', 'κ': '\\kappa', 'λ': '\\lambda', 'μ': '\\mu', 'ν': '\\nu', 'ξ': '\\xi', 'ο': '\\omicron', 'π': '\\pi', 'ρ': '\\rho', 'σ': '\\sigma', 'τ': '\\tau', 'υ': '\\upsilon', 'φ': '\\phi', 'χ': '\\chi', 'ψ': '\\psi', 'ω': '\\omega', 'Α': '\\Alpha', 'Β': '\\Beta', 'Γ': '\\Gamma', 'Δ': '\\Delta', 'Ε': '\\Epsilon', 'Ζ': '\\Zeta', 'Η': '\\Eta', 'Θ': '\\Theta', 'Ι': '\\Iota', 'Κ': '\\Kappa', 'Λ': '\\Lambda', 'Μ': '\\Mu', 'Ν': '\\Nu', 'Ξ': '\\Xi', 'Ο': '\\Omicron', 'Π': '\\Pi', 'Ρ': '\\Rho', 'Σ': '\\Sigma', 'Τ': '\\Tau', 'Υ': '\\Upsilon', 'Φ': '\\Phi', 'Χ': '\\Chi', 'Ψ': '\\Psi', 'Ω': '\\Omega', '*': '\\times', '≤': '\\leq', '≥': '\\geq', '≠': '\\neq', '≈': '\\approx', '∞': '\\infty', '∑': '\\sum', '∏': '\\prod', '∫': '\\int', '∂': '\\partial', '∇': '\\nabla', '√': '\\sqrt', '∝': '\\propto', '∞': '\\infty', }
for symbol, latex in symbol_map.items(): text = text.replace(symbol, latex + " ")
return text
def convert_list_item(text): """将列表符号转换为 Markdown 格式""" return '- ' + text[1:].strip()
def extract_formula(paragraph): """尝试从段落中提取公式文本""" xml = paragraph._element.xml tree = ET.fromstring(xml) formula_text = "" for el in tree.iter(): if el.tag == qn('m:oMath'): formula_text += ET.tostring(el, encoding='unicode') return lxml_parse(formula_text) if formula_text else ''
def hasFormula(para): for child in para._element.getchildren(): if child is not None and child.tag.endswith('oMathPara'): return True return False
def save_image_from_run(run, image_dir, image_counter): """ Save the image in a run to the specified directory and return the image link. """ for inline in run.element.findall('.//wp:inline', namespaces={'wp': 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'}): image_rid = inline.find('.//a:blip', namespaces={'a': 'http://schemas.openxmlformats.org/drawingml/2006/main'}).attrib['{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed'] image_part = run.part.related_parts[image_rid] if not os.path.exists(image_dir): os.makedirs(image_dir) image_path = os.path.join(image_dir, f"image_{image_counter}.png") image_path = image_path.replace('\\', '/') with open(image_path, 'wb') as img_file: img_file.write(image_part.blob) return f"![Image {image_counter}]({urllib.parse.quote(image_path)})"
def contains_image(run): """ Check if the run contains an image. """ return 'wp:inline' in run._element.xml
def has_formula_after_run(run): """ Check if a formula (oMath) is present immediately after the run. """ next_sibling = run._element.getnext() if next_sibling is not None and next_sibling.tag.endswith('oMath'): return True return False
def extract_formula_from_sibling(run): """ Extract the formula from the run's next sibling. """ formula_sibling = run._element.getnext() if formula_sibling is not None: """尝试从段落中提取公式文本""" tree = formula_sibling formula_text = "" if tree.tag == qn('m:oMath'): formula_text = ET.tostring(tree, encoding='unicode') return lxml_parse(formula_text) if formula_text else '' for el in tree.iter(): if el.tag == qn('m:oMath'): formula_text += ET.tostring(el, encoding='unicode') return lxml_parse(formula_text) if formula_text else '' return ""
def word_to_markdown(docx_file, md_file): doc = Document(docx_file) image_dir = os.path.splitext(docx_file)[0] + '_images' image_counter = 0 with open(md_file, 'w', encoding='utf-8') as md: next_para = 1 for para in doc.paragraphs: if not para.text: if hasFormula(para): formula_text = extract_formula(para) md.write(f'${formula_text}$') md.write('\n\n') continue elif contains_image(para): md.write(save_image_from_run(para.runs[0], image_dir, image_counter)) image_counter += 1 md.write('\n\n') continue else: next_para = 1 md.write('\n\n') continue text = para.text.strip() paragraph_text = "" if next_para: paragraph_text = '## ' next_para = 0
for run in para.runs: if contains_image(run): paragraph_text += save_image_from_run(run, image_dir, image_counter) image_counter += 1 text = run.text if text.startswith(('•', '➢', ' •', ' ➢')): text = convert_list_item(text) paragraph_text += text if has_formula_after_run(run): paragraph_text += extract_formula_from_sibling(run)
md.write(paragraph_text + '\n\n')
def convert_docs_in_directory(directory): for filename in os.listdir(directory): if filename.endswith('.docx') and not filename.startswith('~$'): docx_path = os.path.join(directory, filename) md_path = os.path.splitext(docx_path)[0] + '.md' word_to_markdown(docx_path, md_path) print(f"Converted {filename} to Markdown")
if __name__ == "__main__": convert_docs_in_directory('./')
|