p2md 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- p2md-0.2.0/LICENSE +21 -0
- p2md-0.2.0/MANIFEST.in +3 -0
- p2md-0.2.0/PKG-INFO +180 -0
- p2md-0.2.0/README.md +144 -0
- p2md-0.2.0/pyproject.toml +52 -0
- p2md-0.2.0/setup.cfg +4 -0
- p2md-0.2.0/src/p2md/__init__.py +22 -0
- p2md-0.2.0/src/p2md/__main__.py +6 -0
- p2md-0.2.0/src/p2md/api.py +138 -0
- p2md-0.2.0/src/p2md/build_md.py +1060 -0
- p2md-0.2.0/src/p2md/cli.py +195 -0
- p2md-0.2.0/src/p2md/config.py +239 -0
- p2md-0.2.0/src/p2md/detect.py +1626 -0
- p2md-0.2.0/src/p2md/middleware.py +104 -0
- p2md-0.2.0/src/p2md/ocr.py +856 -0
- p2md-0.2.0/src/p2md/ocr_batch.swift +159 -0
- p2md-0.2.0/src/p2md/ocr_fix.py +224 -0
- p2md-0.2.0/src/p2md/pdfsource.py +433 -0
- p2md-0.2.0/src/p2md/project.py +91 -0
- p2md-0.2.0/src/p2md.egg-info/PKG-INFO +180 -0
- p2md-0.2.0/src/p2md.egg-info/SOURCES.txt +33 -0
- p2md-0.2.0/src/p2md.egg-info/dependency_links.txt +1 -0
- p2md-0.2.0/src/p2md.egg-info/entry_points.txt +3 -0
- p2md-0.2.0/src/p2md.egg-info/requires.txt +22 -0
- p2md-0.2.0/src/p2md.egg-info/top_level.txt +1 -0
- p2md-0.2.0/tests/test_build_md.py +50 -0
- p2md-0.2.0/tests/test_build_md_lists.py +207 -0
- p2md-0.2.0/tests/test_build_md_tables_colors.py +86 -0
- p2md-0.2.0/tests/test_detect_image_sampling.py +228 -0
- p2md-0.2.0/tests/test_detect_tables_colors.py +253 -0
- p2md-0.2.0/tests/test_inline_color_and_title.py +182 -0
- p2md-0.2.0/tests/test_long_image_page_alloc.py +161 -0
- p2md-0.2.0/tests/test_long_image_title.py +68 -0
- p2md-0.2.0/tests/test_ocr_backends.py +60 -0
- p2md-0.2.0/tests/test_watermark_filter.py +65 -0
p2md-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 p2md contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
p2md-0.2.0/MANIFEST.in
ADDED
p2md-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: p2md
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Local OCR → structured Markdown for scanned books and long screenshots (no LLM, macOS Vision / RapidOCR)
|
|
5
|
+
Author: p2md contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: ocr,markdown,scanned-book,vision,rapidocr
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: Pillow>=10.0
|
|
20
|
+
Requires-Dist: numpy>=1.24
|
|
21
|
+
Requires-Dist: PyYAML>=6.0
|
|
22
|
+
Requires-Dist: scipy>=1.10
|
|
23
|
+
Requires-Dist: rapidocr>=3.10; sys_platform != "darwin"
|
|
24
|
+
Requires-Dist: onnxruntime>=1.20; sys_platform != "darwin"
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
27
|
+
Provides-Extra: build
|
|
28
|
+
Requires-Dist: build>=1.0; extra == "build"
|
|
29
|
+
Requires-Dist: twine>=5.0; extra == "build"
|
|
30
|
+
Provides-Extra: pdf
|
|
31
|
+
Requires-Dist: PyMuPDF>=1.23; extra == "pdf"
|
|
32
|
+
Provides-Extra: rapidocr
|
|
33
|
+
Requires-Dist: rapidocr>=3.10; extra == "rapidocr"
|
|
34
|
+
Requires-Dist: onnxruntime>=1.20; extra == "rapidocr"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# p2md
|
|
38
|
+
|
|
39
|
+
**扫描书 / 长截图 → 结构化 Markdown。纯本地、无 LLM、确定性输出。**
|
|
40
|
+
|
|
41
|
+
`p2md` 把一本书或一篇长截图转换成可发布的章节 Markdown:识别出正文段落、标题、插图与表格,裁出图片并生成规范的 `images/...` 引用。**功能只到 Markdown 为止**——不生成 PDF,不依赖 document-factory。
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
输入:assets/p3.png ... p692.png(或扫描 PDF / 带文字层 PDF)
|
|
45
|
+
输出:output/content/article_0.md ... + output/content/images/*.png
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 特性
|
|
49
|
+
|
|
50
|
+
- **本地 OCR,按平台自动选择**:macOS 使用 Apple Vision(Swift,中文识别质量最佳);其他平台使用 RapidOCR(onnxruntime 小模型,自带中文)。两者输出完全相同的 JSON schema,后续检测逻辑与平台无关。
|
|
51
|
+
- **长图按段落边界切分**:超长截图先按"段间留白 > 行内间距 × 1.8"落刀,避免把段落腰斩。
|
|
52
|
+
- **领域化的版面分析**:从 OCR 坐标恢复正文/标题/插图/表格;针对浅底 App 截图、K 线/行情截图、图内文字泄漏、标题颜色与字号做了专门修正。
|
|
53
|
+
- **确定性输出**:布局与 Markdown 组装是纯函数(无网络、无 LLM),同输入得到字节级一致的产物。
|
|
54
|
+
- **双入口**:CLI 与 Python API 都能跑完整流程。
|
|
55
|
+
|
|
56
|
+
## 安装
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install p2md
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- macOS 需要 `swiftc`(Xcode Command Line Tools);首次运行会编译包内的 `ocr_batch.swift`。
|
|
63
|
+
- 非 macOS 已自动安装 RapidOCR;如需在 macOS 强制使用 RapidOCR:`pip install "p2md[rapidocr]"` 并设 `P2MD_OCR_BACKEND=rapidocr`(或在 `config.yaml` 配 `book.ocr_backend: rapidocr`)。
|
|
64
|
+
- 需要把 PDF 作为输入(渲染页图 / 提取文字层)时安装:`pip install "p2md[pdf]"`。
|
|
65
|
+
|
|
66
|
+
## 快速开始(CLI)
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
p2md init -p ./projects/进化心理学
|
|
70
|
+
# 把截图或 PDF 放进 projects/进化心理学/assets/
|
|
71
|
+
p2md pipeline -p ./projects/进化心理学
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
分步执行:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
p2md ocr -p ./projects/进化心理学 # ① OCR → output/ocr/
|
|
78
|
+
p2md detect -p ./projects/进化心理学 # ② 版面分析 → output/blocks + output/figs
|
|
79
|
+
p2md build -p ./projects/进化心理学 # ③ 组装 Markdown → output/content/
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
其他命令:
|
|
83
|
+
|
|
84
|
+
- `p2md pdftotext -p <项目>`:PDF 有文字层时直接抽取为 Markdown(免 OCR)。
|
|
85
|
+
- `p2md pipeline -p <项目> --force`:忽略 OCR 缓存重跑。
|
|
86
|
+
- `python -m p2md --help`:查看完整帮助。
|
|
87
|
+
|
|
88
|
+
> 兼容命令 `ocr-book-to-md` 与 `p2md` 等价。
|
|
89
|
+
|
|
90
|
+
## Python API
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
import p2md
|
|
94
|
+
|
|
95
|
+
result = p2md.convert(
|
|
96
|
+
"./projects/进化心理学",
|
|
97
|
+
assets_dir=None, # 默认 <project>/assets
|
|
98
|
+
output_dir="./out", # 最终 Markdown 复制到这里(可选)
|
|
99
|
+
force=False,
|
|
100
|
+
)
|
|
101
|
+
print(result.pages, len(result.markdowns))
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- `convert(project_dir, *, assets_dir, output_dir, work_root, start, end, force, chapters, log)` 一次跑完 `ocr → detect → build`。
|
|
105
|
+
- `convert_images(...)` 是同义函数;返回 `ConvertResult(markdowns, images, pages, work_root, warnings)`。
|
|
106
|
+
- 失败抛 `ConvertError`(底层 `SystemExit` 已统一转换)。
|
|
107
|
+
|
|
108
|
+
## 项目结构
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
projects/<书名>/
|
|
112
|
+
├── config.yaml # 项目配置
|
|
113
|
+
├── assets/ # 输入截图 / PDF
|
|
114
|
+
└── output/
|
|
115
|
+
├── ocr/ # ① OCR 产物(pages_json + 中间页图)
|
|
116
|
+
├── blocks/ # ② 每页 blocks JSON
|
|
117
|
+
├── figs/ # ② 裁出的插图/表格
|
|
118
|
+
├── md/ # ③ 章节 Markdown(便于复核)
|
|
119
|
+
└── content/
|
|
120
|
+
├── article_0.md ...
|
|
121
|
+
└── images/ # 裁图,article 内引用 images/xxx.png
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 配置要点(`config.yaml`)
|
|
125
|
+
|
|
126
|
+
```yaml
|
|
127
|
+
book:
|
|
128
|
+
name: "进化心理学"
|
|
129
|
+
assets_dir: assets
|
|
130
|
+
ocr_backend: auto # auto / swift / rapidocr
|
|
131
|
+
force_ocr: false
|
|
132
|
+
split_long_image_max_ratio: 3.0
|
|
133
|
+
split_slice_max_h: 2600
|
|
134
|
+
split_slice_min_h: 1400
|
|
135
|
+
detect:
|
|
136
|
+
min_heading_ratio: 1.28
|
|
137
|
+
bold_thickness_ratio: 0.078
|
|
138
|
+
heading_plain_ratio: 1.18
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
优先级:命令行 > `.env` > `config.yaml` > 默认值。`.env` 的 KEY 是配置点号路径的大写下划线形式(如 `book.name` → `BOOK_NAME`)。
|
|
142
|
+
|
|
143
|
+
## 输出格式
|
|
144
|
+
|
|
145
|
+
每页 `output/blocks/<n>.json`:
|
|
146
|
+
|
|
147
|
+
```json
|
|
148
|
+
{
|
|
149
|
+
"page": 52,
|
|
150
|
+
"body_h": 47.7,
|
|
151
|
+
"blocks": [
|
|
152
|
+
{"type": "heading", "level": 2, "text": "进化思想史上的里程碑事件", "y_top": 120, "y_bottom": 168},
|
|
153
|
+
{"type": "text", "text": "正文段落……", "y_top": 183, "y_bottom": 231},
|
|
154
|
+
{"type": "image", "file": "p52_f1.png", "kind": "table", "caption": "表1.1 …"}
|
|
155
|
+
]
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
章节 Markdown 示例:
|
|
160
|
+
|
|
161
|
+
```markdown
|
|
162
|
+
# 第1章 通往进化心理学的科学运动
|
|
163
|
+
|
|
164
|
+
## 进化思想史上的里程碑事件
|
|
165
|
+
|
|
166
|
+
正文段落……
|
|
167
|
+
|
|
168
|
+

|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## 已知限制
|
|
172
|
+
|
|
173
|
+
- RapidOCR 的中文识别质量低于 Apple Vision;关键项目建议 macOS 跑最终产物。
|
|
174
|
+
- `pdftotext` 需要 PyMuPDF(`p2md[pdf]`);纯扫描 PDF 走 `pipeline` 渲染 OCR。
|
|
175
|
+
- 图片裁切针对正文 45–50px 的书籍调校;字号差异大时调整 `detect.*` 阈值。
|
|
176
|
+
- 不生成 PDF;如需 PDF 请在下游工具中消费 `output/content/`。
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT
|
p2md-0.2.0/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# p2md
|
|
2
|
+
|
|
3
|
+
**扫描书 / 长截图 → 结构化 Markdown。纯本地、无 LLM、确定性输出。**
|
|
4
|
+
|
|
5
|
+
`p2md` 把一本书或一篇长截图转换成可发布的章节 Markdown:识别出正文段落、标题、插图与表格,裁出图片并生成规范的 `images/...` 引用。**功能只到 Markdown 为止**——不生成 PDF,不依赖 document-factory。
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
输入:assets/p3.png ... p692.png(或扫描 PDF / 带文字层 PDF)
|
|
9
|
+
输出:output/content/article_0.md ... + output/content/images/*.png
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 特性
|
|
13
|
+
|
|
14
|
+
- **本地 OCR,按平台自动选择**:macOS 使用 Apple Vision(Swift,中文识别质量最佳);其他平台使用 RapidOCR(onnxruntime 小模型,自带中文)。两者输出完全相同的 JSON schema,后续检测逻辑与平台无关。
|
|
15
|
+
- **长图按段落边界切分**:超长截图先按"段间留白 > 行内间距 × 1.8"落刀,避免把段落腰斩。
|
|
16
|
+
- **领域化的版面分析**:从 OCR 坐标恢复正文/标题/插图/表格;针对浅底 App 截图、K 线/行情截图、图内文字泄漏、标题颜色与字号做了专门修正。
|
|
17
|
+
- **确定性输出**:布局与 Markdown 组装是纯函数(无网络、无 LLM),同输入得到字节级一致的产物。
|
|
18
|
+
- **双入口**:CLI 与 Python API 都能跑完整流程。
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install p2md
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- macOS 需要 `swiftc`(Xcode Command Line Tools);首次运行会编译包内的 `ocr_batch.swift`。
|
|
27
|
+
- 非 macOS 已自动安装 RapidOCR;如需在 macOS 强制使用 RapidOCR:`pip install "p2md[rapidocr]"` 并设 `P2MD_OCR_BACKEND=rapidocr`(或在 `config.yaml` 配 `book.ocr_backend: rapidocr`)。
|
|
28
|
+
- 需要把 PDF 作为输入(渲染页图 / 提取文字层)时安装:`pip install "p2md[pdf]"`。
|
|
29
|
+
|
|
30
|
+
## 快速开始(CLI)
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
p2md init -p ./projects/进化心理学
|
|
34
|
+
# 把截图或 PDF 放进 projects/进化心理学/assets/
|
|
35
|
+
p2md pipeline -p ./projects/进化心理学
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
分步执行:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
p2md ocr -p ./projects/进化心理学 # ① OCR → output/ocr/
|
|
42
|
+
p2md detect -p ./projects/进化心理学 # ② 版面分析 → output/blocks + output/figs
|
|
43
|
+
p2md build -p ./projects/进化心理学 # ③ 组装 Markdown → output/content/
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
其他命令:
|
|
47
|
+
|
|
48
|
+
- `p2md pdftotext -p <项目>`:PDF 有文字层时直接抽取为 Markdown(免 OCR)。
|
|
49
|
+
- `p2md pipeline -p <项目> --force`:忽略 OCR 缓存重跑。
|
|
50
|
+
- `python -m p2md --help`:查看完整帮助。
|
|
51
|
+
|
|
52
|
+
> 兼容命令 `ocr-book-to-md` 与 `p2md` 等价。
|
|
53
|
+
|
|
54
|
+
## Python API
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import p2md
|
|
58
|
+
|
|
59
|
+
result = p2md.convert(
|
|
60
|
+
"./projects/进化心理学",
|
|
61
|
+
assets_dir=None, # 默认 <project>/assets
|
|
62
|
+
output_dir="./out", # 最终 Markdown 复制到这里(可选)
|
|
63
|
+
force=False,
|
|
64
|
+
)
|
|
65
|
+
print(result.pages, len(result.markdowns))
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
- `convert(project_dir, *, assets_dir, output_dir, work_root, start, end, force, chapters, log)` 一次跑完 `ocr → detect → build`。
|
|
69
|
+
- `convert_images(...)` 是同义函数;返回 `ConvertResult(markdowns, images, pages, work_root, warnings)`。
|
|
70
|
+
- 失败抛 `ConvertError`(底层 `SystemExit` 已统一转换)。
|
|
71
|
+
|
|
72
|
+
## 项目结构
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
projects/<书名>/
|
|
76
|
+
├── config.yaml # 项目配置
|
|
77
|
+
├── assets/ # 输入截图 / PDF
|
|
78
|
+
└── output/
|
|
79
|
+
├── ocr/ # ① OCR 产物(pages_json + 中间页图)
|
|
80
|
+
├── blocks/ # ② 每页 blocks JSON
|
|
81
|
+
├── figs/ # ② 裁出的插图/表格
|
|
82
|
+
├── md/ # ③ 章节 Markdown(便于复核)
|
|
83
|
+
└── content/
|
|
84
|
+
├── article_0.md ...
|
|
85
|
+
└── images/ # 裁图,article 内引用 images/xxx.png
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 配置要点(`config.yaml`)
|
|
89
|
+
|
|
90
|
+
```yaml
|
|
91
|
+
book:
|
|
92
|
+
name: "进化心理学"
|
|
93
|
+
assets_dir: assets
|
|
94
|
+
ocr_backend: auto # auto / swift / rapidocr
|
|
95
|
+
force_ocr: false
|
|
96
|
+
split_long_image_max_ratio: 3.0
|
|
97
|
+
split_slice_max_h: 2600
|
|
98
|
+
split_slice_min_h: 1400
|
|
99
|
+
detect:
|
|
100
|
+
min_heading_ratio: 1.28
|
|
101
|
+
bold_thickness_ratio: 0.078
|
|
102
|
+
heading_plain_ratio: 1.18
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
优先级:命令行 > `.env` > `config.yaml` > 默认值。`.env` 的 KEY 是配置点号路径的大写下划线形式(如 `book.name` → `BOOK_NAME`)。
|
|
106
|
+
|
|
107
|
+
## 输出格式
|
|
108
|
+
|
|
109
|
+
每页 `output/blocks/<n>.json`:
|
|
110
|
+
|
|
111
|
+
```json
|
|
112
|
+
{
|
|
113
|
+
"page": 52,
|
|
114
|
+
"body_h": 47.7,
|
|
115
|
+
"blocks": [
|
|
116
|
+
{"type": "heading", "level": 2, "text": "进化思想史上的里程碑事件", "y_top": 120, "y_bottom": 168},
|
|
117
|
+
{"type": "text", "text": "正文段落……", "y_top": 183, "y_bottom": 231},
|
|
118
|
+
{"type": "image", "file": "p52_f1.png", "kind": "table", "caption": "表1.1 …"}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
章节 Markdown 示例:
|
|
124
|
+
|
|
125
|
+
```markdown
|
|
126
|
+
# 第1章 通往进化心理学的科学运动
|
|
127
|
+
|
|
128
|
+
## 进化思想史上的里程碑事件
|
|
129
|
+
|
|
130
|
+
正文段落……
|
|
131
|
+
|
|
132
|
+

|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 已知限制
|
|
136
|
+
|
|
137
|
+
- RapidOCR 的中文识别质量低于 Apple Vision;关键项目建议 macOS 跑最终产物。
|
|
138
|
+
- `pdftotext` 需要 PyMuPDF(`p2md[pdf]`);纯扫描 PDF 走 `pipeline` 渲染 OCR。
|
|
139
|
+
- 图片裁切针对正文 45–50px 的书籍调校;字号差异大时调整 `detect.*` 阈值。
|
|
140
|
+
- 不生成 PDF;如需 PDF 请在下游工具中消费 `output/content/`。
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "p2md"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Local OCR → structured Markdown for scanned books and long screenshots (no LLM, macOS Vision / RapidOCR)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{name = "p2md contributors"}]
|
|
14
|
+
keywords = ["ocr", "markdown", "scanned-book", "vision", "rapidocr"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Text Processing :: Markup :: Markdown",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"Pillow>=10.0",
|
|
27
|
+
"numpy>=1.24",
|
|
28
|
+
"PyYAML>=6.0",
|
|
29
|
+
"scipy>=1.10",
|
|
30
|
+
"rapidocr>=3.10; sys_platform != 'darwin'",
|
|
31
|
+
"onnxruntime>=1.20; sys_platform != 'darwin'",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
test = ["pytest>=7.0"]
|
|
36
|
+
build = ["build>=1.0", "twine>=5.0"]
|
|
37
|
+
pdf = ["PyMuPDF>=1.23"]
|
|
38
|
+
rapidocr = ["rapidocr>=3.10", "onnxruntime>=1.20"]
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
p2md = "p2md.cli:main"
|
|
42
|
+
ocr-book-to-md = "p2md.cli:main"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.package-data]
|
|
48
|
+
p2md = ["ocr_batch.swift"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
testpaths = ["tests"]
|
|
52
|
+
pythonpath = ["src"]
|
p2md-0.2.0/setup.cfg
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""p2md —— 扫描书 / 长截图转结构化 Markdown(本地、无 LLM)。
|
|
3
|
+
|
|
4
|
+
三段流水线(每步产物落盘、JSON 协议解耦):
|
|
5
|
+
① ocr OCR → output/ocr/(macOS 用 Swift Vision,其他平台用 RapidOCR)
|
|
6
|
+
② detect 图/标题/正文三分检测 → output/blocks + output/figs
|
|
7
|
+
③ build 组装章节 Markdown → output/content/article_N.md + images/
|
|
8
|
+
|
|
9
|
+
功能边界:只产出 Markdown 与裁图,不生成 PDF,不依赖 document-factory。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .api import ConvertError, ConvertResult, convert, convert_images
|
|
13
|
+
|
|
14
|
+
__version__ = "0.2.0"
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"ConvertError",
|
|
18
|
+
"ConvertResult",
|
|
19
|
+
"convert",
|
|
20
|
+
"convert_images",
|
|
21
|
+
"__version__",
|
|
22
|
+
]
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""库接口:把一个图片集从页图转成章节 Markdown,一次调用走完 ocr → detect → build。
|
|
3
|
+
|
|
4
|
+
- CLI(``main.py``)面向"一个项目一本书",路径写死在项目根下;
|
|
5
|
+
- 库调用时源页图可来自任意目录,中间产物落在调用方给定的工作目录,
|
|
6
|
+
最终 Markdown 可复制到指定输出目录。
|
|
7
|
+
|
|
8
|
+
约定:
|
|
9
|
+
- 中间产物(ocr/blocks/figs/md)落 ``work_root``,不污染项目根;
|
|
10
|
+
- 最终章节 Markdown 从 build 的 article 目录复制到 ``output_dir``(给了才复制),
|
|
11
|
+
图片一并复制到 ``<output_dir>/images/``,md 里的 ``images/...`` 引用即可直接解析。
|
|
12
|
+
|
|
13
|
+
错误处理:底层阶段用 ``SystemExit`` 表示"前置条件不满足"(缺图、缺 blocks 等),
|
|
14
|
+
这里统一转成 ``ConvertError``,避免库调用把整个进程带走。
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import shutil
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Callable, List, Optional
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ConvertError(RuntimeError):
|
|
26
|
+
"""转换失败(输入缺失、工具不可用、阶段报错)。调用方据此判 failed/invalid。"""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class ConvertResult:
|
|
31
|
+
"""一次图片集转换的结果。``markdowns`` 是最终可用的章节文件(已含图片相对引用)。"""
|
|
32
|
+
|
|
33
|
+
markdowns: List[Path] = field(default_factory=list)
|
|
34
|
+
images: List[Path] = field(default_factory=list)
|
|
35
|
+
pages: int = 0
|
|
36
|
+
work_root: Optional[Path] = None
|
|
37
|
+
warnings: List[str] = field(default_factory=list)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _call(stage: str, fn, *args, **kwargs):
|
|
41
|
+
"""把阶段里的 SystemExit / 异常收敛成 ConvertError,并带上是哪一步挂的。"""
|
|
42
|
+
try:
|
|
43
|
+
return fn(*args, **kwargs)
|
|
44
|
+
except ConvertError:
|
|
45
|
+
raise
|
|
46
|
+
except SystemExit as exc: # 底层用 SystemExit 表达前置条件不满足
|
|
47
|
+
raise ConvertError(f"{stage}: {exc}") from exc
|
|
48
|
+
except Exception as exc: # noqa: BLE001 - 统一收敛给调用方判定
|
|
49
|
+
raise ConvertError(f"{stage}: {type(exc).__name__}: {exc}") from exc
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def convert_images(project_dir, *, assets_dir=None, output_dir=None, work_root=None,
|
|
53
|
+
start=None, end=None, force=False, chapters=None,
|
|
54
|
+
log: Optional[Callable[[str], None]] = None) -> ConvertResult:
|
|
55
|
+
"""把一个图片集转成章节 Markdown。
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
project_dir: 项目根(读 ``config.yaml`` / ``.env``)。
|
|
59
|
+
assets_dir: 源页图目录,默认 ``<project_dir>/assets``。
|
|
60
|
+
output_dir: 最终 Markdown 落地目录;不给就只留在 work_root 的 article 目录。
|
|
61
|
+
work_root: 中间产物目录,默认 ``<project_dir>/.derived/ocr/<assets 目录名>``。
|
|
62
|
+
start/end: 页码范围(None=自动)。
|
|
63
|
+
force: 忽略 OCR 缓存重跑。
|
|
64
|
+
chapters: 人工章节结构(透传 build)。
|
|
65
|
+
log: 日志回调,默认不打印(库调用不该刷屏)。
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
ConvertResult。
|
|
69
|
+
"""
|
|
70
|
+
from . import build_md, detect as detect_mod, middleware, ocr
|
|
71
|
+
from .project import Project
|
|
72
|
+
|
|
73
|
+
say = log or (lambda _msg: None)
|
|
74
|
+
root = Path(project_dir).expanduser().resolve()
|
|
75
|
+
if not root.is_dir():
|
|
76
|
+
raise ConvertError(f"项目目录不存在: {root}")
|
|
77
|
+
|
|
78
|
+
src = Path(assets_dir).expanduser().resolve() if assets_dir else root / "assets"
|
|
79
|
+
if not src.is_dir():
|
|
80
|
+
raise ConvertError(f"图片集源目录不存在: {src}")
|
|
81
|
+
|
|
82
|
+
work = (Path(work_root).expanduser().resolve() if work_root
|
|
83
|
+
else root / ".derived" / "ocr" / src.name)
|
|
84
|
+
work.mkdir(parents=True, exist_ok=True)
|
|
85
|
+
|
|
86
|
+
project = Project(str(root), output_dir=str(work), assets_dir=str(src))
|
|
87
|
+
|
|
88
|
+
say(f"[ocr] 源 {src} → 中间产物 {work}")
|
|
89
|
+
img_dir, _start, _end = _call("ocr", ocr.run, project, str(src),
|
|
90
|
+
start, end, force_ocr=force)
|
|
91
|
+
|
|
92
|
+
say("[middleware] 逐页修正")
|
|
93
|
+
_call("middleware", middleware.run_all, project)
|
|
94
|
+
|
|
95
|
+
say("[detect] 版面检测")
|
|
96
|
+
_call("detect", detect_mod.detect, project, img_dir, None)
|
|
97
|
+
|
|
98
|
+
say("[build] 组装章节")
|
|
99
|
+
md_dir, article_dir, written = _call("build", build_md.build, project, chapters)
|
|
100
|
+
|
|
101
|
+
bodies = sorted(Path(article_dir).glob("*.md"))
|
|
102
|
+
images = sorted((Path(article_dir) / "images").glob("*")) \
|
|
103
|
+
if (Path(article_dir) / "images").is_dir() else []
|
|
104
|
+
|
|
105
|
+
pages_json = project.get_ocr_pages_json_dir()
|
|
106
|
+
pages = len(list(pages_json.glob("*.json"))) if pages_json.is_dir() else 0
|
|
107
|
+
|
|
108
|
+
result = ConvertResult(markdowns=bodies, images=images, pages=pages, work_root=work)
|
|
109
|
+
|
|
110
|
+
if output_dir:
|
|
111
|
+
dest = Path(output_dir).expanduser().resolve()
|
|
112
|
+
dest.mkdir(parents=True, exist_ok=True)
|
|
113
|
+
for path in bodies:
|
|
114
|
+
shutil.copyfile(path, dest / path.name)
|
|
115
|
+
if images:
|
|
116
|
+
img_dest = dest / "images"
|
|
117
|
+
img_dest.mkdir(parents=True, exist_ok=True)
|
|
118
|
+
for path in images:
|
|
119
|
+
shutil.copyfile(path, img_dest / path.name)
|
|
120
|
+
result.markdowns = [dest / p.name for p in bodies]
|
|
121
|
+
result.images = [dest / "images" / p.name for p in images]
|
|
122
|
+
say(f"[done] {len(bodies)} 章 → {dest}")
|
|
123
|
+
|
|
124
|
+
if not result.markdowns:
|
|
125
|
+
result.warnings.append("没有产出任何 Markdown(检查页图与章节结构)")
|
|
126
|
+
return result
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def convert(project_dir, *, assets_dir=None, output_dir=None, work_root=None,
|
|
130
|
+
start=None, end=None, force=False, chapters=None,
|
|
131
|
+
log: Optional[Callable[[str], None]] = None) -> ConvertResult:
|
|
132
|
+
""":func:`convert_images` 的短别名:``p2md.convert(project, ...)`` 一句话转完。"""
|
|
133
|
+
return convert_images(project_dir, assets_dir=assets_dir, output_dir=output_dir,
|
|
134
|
+
work_root=work_root, start=start, end=end, force=force,
|
|
135
|
+
chapters=chapters, log=log)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
__all__ = ["ConvertError", "ConvertResult", "convert", "convert_images"]
|