_docx_converter.py · _xlsx_converter.py · _pptx_converter.py · converter_utils/docx/三個 Office 格式走完全不同路線:DOCX 先經 mammoth 轉 HTML 再進
_markdownify;XLSX 用 pandas 讀表;PPTX 用
python-pptx 走版面物件。最精彩的是 converter_utils/docx——把 Word 的 OMML 數學轉成 LaTeX。
constructor 先跑 pre_process_docx()(把 OMML 數學換成 LaTeX 佔位),
再叫 mammoth 轉 HTML,最後用繼承自 HtmlConverter 的 _markdownify 轉 Markdown。
docstring:標題與表格盡可能保留。
<m:oMath> 轉成 LaTeX 佔位。docx 是 zip,內含 word/document.xml。流程:解壓 → 用 BeautifulSoup 掃 XML → 找到
oMath 元素 → 用 oMath2Latex 轉成 $$...$$ LaTeX →
再打包回 docx。因為 mammoth 本身不認得 OMML,先換成文字再進 mammoth 是唯一出路。
這是個「標籤分派」直譯器:Tag2Method 依 XML tag 名呼叫對應方法
(do_sub/do_sup/do_rad/do_f/do_nary/do_acc/do_bar/do_lim/do_eqarr/do_mr…)。
oMath2Latex 負責整體轉換;Pr 處理數學屬性(樣式)。graphify 掃描顯示
oMath2Latex 是全圖第六大 god node(31 邊)——這是「分數/根號/上下標」轉換的功臣。
## 表名 + Markdown 表格。用 pandas.read_excel(engine="openpyxl"/"xlrd") 讀所有 sheet,每個 sheet
輸出標題 + 表頭 + 資料列。注意:pandas 會把整張表的內容(含合併列、註解文字)都帶進表格,
並以 NaN 呈現空值——這是「誠實但可能不像人寫」的輸出(見案例頁觀察)。
對每張 slide 的每個 shape 分派:
_is_picture — 圖片 shape → 輸出 (可配 llm_caption)。_is_table — 表格 shape → _convert_table_to_markdown。_convert_chart_to_markdown。_find_svg_blip_part — 處理 <asvg:svgBlip> 的 SVG 圖片(避免 raster 依賴)。每個 slide 開頭輸出 <!-- Slide number: N --> 註解。