离线 · 无上传 · 浏览器本地完成Offline · no uploads · runs in the browser
Word 转 JSON——离线、有表才成数组、多表会分组Word to JSON offline — tables, grouping, no guesswork
这条链的产出形状完全由文档里**有几张表**决定,这是它最容易被讲反的地方。`docx>html` 先由 mammoth 把文档体还原成语义 HTML(标题层级、列表、表格;内嵌图片读成 data URI;脚注会作为末尾列表进来、插入修订按正文读出、文本框内容照常成段,只有批注不落),`html>json` 再按三条分支取数:**只有一张表**时产出对象数组——第一行当表头、其余行按表头配成键值;**两张及以上**时产出**按文档顺序分组的对象**,分组名是把表与文档里的 `h2` **按出现顺序逐位配对**(第 N 张表配第 N 个 `h2`),配不上——也就是标题不够用——才回落成 `Sheet2` 这类名字,所以「某张表的组名」和「它上方那个小标题」并不总是一回事,表比标题多或中间夹着别的标题时就会错位;**一张表都没有**时退一步取文字:先找第一个 `<pre>`,没有再找第一个 `<code>`,两个都没有就用整份 `<body>` 的文字,然后 `JSON.parse`——注意这三处取的都是 `textContent`,所以用 `<br>` 排的换行在这里会消失;三者都解析不了才明确报 `errors.htmlToJson`,而不是给你一个空数组。有一条重要的下游连带后果要写在这:多张表产出的**不是数组**,所以紧接着那一步「JSON 转 CSV」只吃数组就会报错「JSON 内容必须是包含数据的数组才能转换为 CSV」。这里要说清一件容易想反的事:**Word→Excel 也经过同一步**(基线现读:`docx>html` → `html>json` → `json>csv` → `csv>xlsx`),所以一份多表文档转 Excel 会在同一个位置报错,并不存在「绕过那道闸」的现成路线——要一次拿多张表,只能把每张表各存成一份只含一表的文件分别转,或者走 Word→HTML 之后自己取。取值也有边界:mammoth 读的是文档里存下的文字,Word 表格中算好并存下的公式结果会以文字进来、但不会被重新求值;单元格格式与条件格式不进 JSON。表头里重复或为空的格子会互相覆盖(后者赢),跨 `colspan` 的表头只算一格。全程只有 storage 权限,不联网。The shape of the output on this chain is decided entirely by **how many tables the document holds**, and that is where it most often gets explained backwards. `docx>html` first has mammoth restore the body as semantic HTML (heading levels, lists, tables; embedded pictures become data URIs; footnotes arrive as a trailing list, tracked insertions read as body text and text-box content becomes paragraphs — only comments are left out), and `html>json` then takes one of three branches. With **exactly one table** in the parsed document it produces an array of objects, the first row as headers and the rest zipped against them. With **two or more** it produces an **object grouped in document order**, where group names come from pairing the tables with the page’s `h2` elements **positionally, in order** — the Nth table takes the Nth heading — and only when that pairing runs out, i.e. there are not enough headings, does it fall back to names like `Sheet2`; so «the name of a table’s group» and «the heading above that table» are not the same thing, and they drift apart as soon as tables outnumber the headings or another heading sits in between. With **no table at all** it falls back to text: the first `<pre>`, else the first `<code>`, else the whole `<body>`’s text, and runs `JSON.parse` on it — all three read `textContent`, so line breaks made with `<br>` disappear here — and only when none of the three parses does it fail with `errors.htmlToJson` rather than handing you an empty array. One downstream consequence belongs on this page. Several tables produce **not an array**, so the very next step, JSON-to-CSV, which only accepts arrays, refuses with «JSON content must be an array of objects to convert to CSV». And here is the thing most routes get backwards: **Word to Excel passes through that same step** — read live from the baseline, `docx>html` then `html>json` then `json>csv` then `csv>xlsx` — so a multi-table document fails at the same place on the way to Excel too, and there is no ready-made route that dodges the gate. To get several tables out, convert one table per file, or take Word to HTML and pick them apart yourself. The value boundary is stated too: mammoth reads the words stored in the document, so a formula result already computed and stored in a Word table arrives as text but is never re-evaluated, and cell formatting including conditional formatting does not reach the JSON. Duplicate or empty header cells overwrite each other (the later one wins), and a header spanning `colspan` counts as one key. All of it on the storage permission, with no network.
这条链路怎么走How the route runs
Word (.docx)Word (.docx) HTMLHTML JSONJSON
共 2 步,由扩展的路径搜索自动选出,中间产物不落盘。2 steps, chosen by the extension's own path search; intermediate results never touch the disk.
会保留什么What carries over
- 一张表就是对象数组:第一行当表头、其余行按键配对,顺序保持文档原样One table becomes an array of objects: the first row as headers, the rest zipped against them, in document order
- 多张表按文档顺序分组,组名与第 N 个 `h2` 逐位配对;配对用完才回落成 `Sheet2` 这类名字Several tables group in document order, each name paired positionally with the Nth `h2`; only a pairing that runs out falls back to `Sheet2`-style names
- 没有表格时依次试第一个 `<pre>`、第一个 `<code>`、整份正文文字,再解析;解析不了就明确报错With no table it tries, in order, the first `<pre>`, the first `<code>` and the whole body text, and fails clearly when none of them parses
- 标题层级与正文文字都按语义还原(`h1`–`h4`、列表、表格),不是把文档压成一坨文本Heading levels and body text are restored semantically (`h1`–`h4`, lists, tables) rather than flattened into one blob
- 一批最多 200 个文件、单个 100 MB,与其他路线共用同一道闸门Up to 200 files per batch and 100 MB each — the same gate every other route shares
如实说明的限制What it does not do
- 多张表产出的是分组对象而不是数组,下一步「JSON 转 CSV」只吃数组就会报错——**Word→Excel 也经过这一步**,同样报错;请一张表转一次,或走 Word→HTML 自己取Several tables yield a grouped object, not an array, so the JSON-to-CSV step refuses — and **Word to Excel runs through that same step**, failing at the same place; convert one table at a time or take Word to HTML
- 取的是文档里存下的文字:Word 表格里算好的公式结果会以文字进来但不会重新求值,单元格格式与条件格式不进 JSONIt reads the words stored in the document: a formula result already computed inside a Word table arrives as text but is never re-evaluated, and cell formatting including conditional formatting does not reach the JSON
- 表头里重复或为空的格子会互相覆盖(后者赢),跨 `colspan` 的表头只算一个键Duplicate or empty header cells overwrite each other (the later wins), and a header spanning `colspan` counts as a single key
- 用 `<br>` 排的换行在退读文字那一步会消失(三处取的都是 `textContent`)Line breaks made with `<br>` vanish in the text fallback, where all three sources read `textContent`
- 脚注会以 `[1]` 这样的引用留在文字里;批注不进、`w:ins` 按正文读出Footnote references stay in the text as `[1]`-style marks; comments do not come across and `w:ins` reads as body text
实操提示Working with it
- 要表格数据:一张表时 Word→CSV 或 Word→Excel 都能走;多张表时这两条也会在同一步报错,请先拆开For table data: with one table, Word to CSV or Word to Excel both work; with several, those routes fail at the same step too, so split them first
- 要纯文字:Word→TXT 丢掉表格结构,但正文读得更完整For plain words: Word to TXT drops the table structure but reads the body more completely
- 要能再编辑的网页:Word→HTML 会把合并写成 colspan / rowspan 留在标记里;Word→Markdown 则把格子摊平成纯文本,合并信息到那一步就没了For an editable page: Word to HTML keeps merges in the markup as colspan / rowspan, while Word to Markdown flattens cells to plain text and the merge is gone at that step
常见问题Questions people actually ask
为什么转出来是一个对象而不是数组?Why did I get an object instead of an array?
因为文档里有两张以上的表(表格套在单元格里也算两张,取表用的是会下钻的选择器)。这条路会按文档顺序把它们分组,组名与文档里的 `h2` 逐位配对,配对用完才用 `SheetN`。这是刻意保留「哪张表是哪个」的信息,代价就是产物不再是数组——而紧接着的「JSON 转 CSV」只吃数组,所以它报错。Word→Excel 也经过同一步(基线现读四步链),所以多表文档转 Excel 会在同一个位置失败,没有绕得开的现成路线;要么一次转一张表,要么走 Word→HTML 之后自己取。Because the document held more than one table (a table nested inside a cell counts too, since the selector descends). The route groups them in document order, pairing each name with the Nth `h2` and only falling back to `SheetN` once that pairing runs out. Keeping «which table is which» is deliberate, and the price is that the artifact is no longer an array — which is exactly what the next step, JSON-to-CSV, complains about. Word to Excel goes through that same step (a four-step chain, read live from the baseline), so a multi-table document fails at the same place there as well and no ready-made route dodges it. Convert one table at a time, or take Word to HTML and separate them yourself.
我的 Word 里没有表格,转 JSON 会怎样?What if my Word file has no tables?
取表那一步落空后会依次退三档:先读第一个 `<pre>` 的文字,没有再读第一个 `<code>`,两个都没有就读整份 `<body>` 的文字,然后 `JSON.parse`。也就是说文档里得有形如 JSON 的文字才行——三档读的都是 `textContent`,用 `<br>` 排的换行会在这一步消失。三档都解析不了才明确报 `errors.htmlToJson`,不会给你一份空数组冒充成功。要的是文字,请走 Word→TXT / Markdown。When the table branch misses it falls back in three stages: the text of the first `<pre>`, else the first `<code>`, else the text of the whole `<body>`, and that string goes through `JSON.parse`. So the document needs text that really looks like JSON — and all three stages read `textContent`, which means line breaks made with `<br>` are gone by then. Only when none of the three parses does it fail with `errors.htmlToJson` rather than passing off an empty array as success. If what you want is words, take Word to TXT or Markdown.
表头那一行会怎么处理?How is the header row handled?
一张表的场合取第一行当键名、其余行配成对象。三个细节值得知道:表头里出现重复或为空的格子,后写的会盖掉先写的;跨 `colspan` 的表头只算一个键;某一行格子比表头多时,多出的部分是在取表这一步就被丢掉的(不是后面转 CSV 时丢的)。要参与运算的值请在 Word 里算完再转——公式不会被重新求值。With a single table, the first row supplies the keys and the rest become objects. Three details matter: repeated or empty header cells let the later one win, a header spanning `colspan` counts as one key, and when a row carries more cells than the header, the extras are dropped right here at the table-reading step (not later, converting to CSV). Values that must be computed belong in Word first — formulas are never re-evaluated.
不需要账号,也不需要联网:装好扩展后点图标,在新标签页里打开工作台,文件从磁盘直接选。No account and no network: install the extension, click its icon, and the workbench opens in a new tab reading straight off your disk.
从 Chrome 应用商店安装Install from the Chrome Web Store 安装步骤Install steps GitHub 源码Source on GitHub