Files
wordtable2sqlite/README.md
2026-06-24 16:31:35 +08:00

86 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# wordtable2sqlite
`.docx` 里的 Word 表格内容导入 SQLite 的最小 Go 工具。
详细操作说明见:[HOWTO.md](HOWTO.md)
## 特点
- 不依赖 Word、WPS、Python 或 LibreOffice。
- 直接解析 `.docx` 内部的 `word/document.xml`
- SQLite 使用纯 Go 驱动 `modernc.org/sqlite`,方便编译成单文件可执行程序。
- 保底保存所有表格单元格到 `cells`
- 对常见报备表这种“标题行 + 表头行 + 数据行”的结构,自动生成 `fields`,便于按字段检索。
- 内置导出 `fields` / `cells` CSV不要求普通用户额外安装 `sqlite3`
## 编译
```bash
go build -o bin/wordtable2sqlite .
```
给非技术用户使用时,分发 `bin/wordtable2sqlite` 这个可执行文件即可。
## 使用
```bash
./bin/wordtable2sqlite -reset -db data/report.sqlite input.docx
```
批量导入多个文件:
```bash
./bin/wordtable2sqlite -db data/report.sqlite *.docx
```
导出字段化内容 CSV
```bash
./bin/wordtable2sqlite export-fields -db data/report.sqlite -out data/report-fields.csv
```
导出原始单元格 CSV
```bash
./bin/wordtable2sqlite export-cells -db data/report.sqlite -out data/report-cells.csv
```
参数:
- `-db`:输出 SQLite 文件路径,默认 `word_tables.sqlite`
- `-reset`:导入前删除旧数据库
- `-quiet`:不输出每个文件的摘要
## 适用范围
当前字段推断优先覆盖常见报备表:一行标题、一行表头、一行或多行数据。横向合并的表头会按逻辑列范围合并对应值。复杂多行表头、纵向合并、嵌套表格仍建议同时查看 `cells` 原始单元格结果。
## 数据表
- `documents`:每个导入的 Word 文件。
- `tables`Word 文档中的表格。
- `cells`:每个表格单元格,包含表格序号、行号、列号、跨列数和文本。
- `fields`:自动推断出的字段,常用于报备表检索。
常用查询:
```sql
SELECT key, value
FROM fields
ORDER BY id;
```
```sql
SELECT table_index, row_index, cell_index, text
FROM cells
ORDER BY table_index, row_index, cell_index;
```
## 验证
```bash
go test ./...
go vet ./...
go build -o bin/wordtable2sqlite .
```