dsh-pdf-workbench 1.3.3 → 1.4.0
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.
- package/LICENSE +21 -21
- package/README.md +225 -199
- package/assets/screenshot-attachments.jpg +0 -0
- package/assets/screenshot-canvas.jpg +0 -0
- package/assets/screenshot-questions.jpg +0 -0
- package/assets/screenshot-settings-data.jpg +0 -0
- package/assets/screenshot-settings-mode.jpg +0 -0
- package/assets/screenshot-settings-reading.jpg +0 -0
- package/client.js +5700 -849
- package/docs/DETAIL.md +1 -69
- package/docs/RELEASE.md +119 -119
- package/docs/SIDE-PREVIEW.md +5 -1
- package/docs/preview.svg +86 -0
- package/icon.svg +7 -6
- package/index.js +18 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
- package/screenshots.json +13 -8
- package/src/client/host-ui-entry.js +11 -11
- package/src/client/react-jsx-runtime.cjs +21 -21
- package/src/client/viewer.js +80 -80
- package/src/host/artifacts.js +31 -3
- package/src/host/attachments.js +159 -0
- package/src/host/backup.js +457 -0
- package/src/host/base-session.js +146 -0
- package/src/host/book-import.js +98 -0
- package/src/host/chapter-sessions.js +154 -0
- package/src/host/chapter-text.js +91 -0
- package/src/host/fonts.js +190 -0
- package/src/host/pdf-stream.js +168 -0
- package/src/host/ppt.js +153 -0
- package/src/host/print.js +191 -0
- package/src/host/questions.js +186 -0
- package/src/host/routes.js +671 -25
- package/src/host/state.js +1 -1
- package/src/host/zip.js +186 -0
- package/tools/AGENTS.md +33 -33
- package/tools/README.md +138 -138
- package/tools/auto_chapters.py +122 -122
- package/tools/dsfile.mjs +198 -198
- package/tools/export_chapter.py +108 -108
- package/tools/export_page.py +91 -91
- package/tools/md2overlay.py +200 -200
- package/tools/md2tex.py +422 -422
- package/tools/pdf_blocks.py +110 -110
- package/tools/pdf_fontsize.py +20 -20
- package/tools/pdf_merge.py +15 -15
- package/tools/pdf_pagesizes.py +8 -8
- package/tools/pdf_raster.py +21 -21
- package/tools/pdf_text.py +34 -0
- package/tools/pdfkit.py +896 -896
- package/tools/realign_overlay.py +248 -248
- package/tools/rebuild_overlay.py +55 -55
- package/tools/rename_chapters.py +93 -93
- package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -142
- package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -204
- package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -61
- package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -77
- package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -11
- package/vendor/cmaps/LICENSE.txt +36 -36
- package/vendor/fonts/excalifont/OFL.txt +98 -98
- package/vendor/fonts/zhhand/OFL.txt +99 -99
- package/vendor/host-ui.cjs +1719 -1719
- package/vendor/host-ui.css +799 -799
- package/vendor/pdf.min.mjs +28 -28
- package/vendor/pdf.worker.min.mjs +28 -28
- package/vendor/pdfjs-LICENSE.txt +177 -177
- package/vendor/rough/LICENSE +21 -21
- package/vendor/rough/rough.mjs +1 -1
- package/vendor/wasm/LICENSE_JBIG2 +196 -196
- package/vendor/wasm/LICENSE_OPENJPEG +39 -39
- package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -13
- package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -22
- package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -19
- package/vendor/wasm/LICENSE_QCMS +21 -21
- package/vendor/wasm/jbig2_nowasm_fallback.js +15 -15
- package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -17
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +1 -1
- package/tools/node.cmd +0 -3
- package/tools/py.cmd +0 -3
- package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
package/tools/pdf_blocks.py
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
"""One PDF page's layout blocks as JSON (PyMuPDF).
|
|
2
|
-
|
|
3
|
-
Usage:
|
|
4
|
-
python pdf_blocks.py <input.pdf> <page-index-1-based>
|
|
5
|
-
|
|
6
|
-
The parsing is also importable: `blocks_for_page(path, number)` returns the same
|
|
7
|
-
list, which is what the paragraph realigner uses instead of spawning this script
|
|
8
|
-
(no pipes, no second interpreter).
|
|
9
|
-
"""
|
|
10
|
-
import json
|
|
11
|
-
import sys
|
|
12
|
-
|
|
13
|
-
import fitz
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def blocks_for_page(pdf_path, page_number):
|
|
17
|
-
"""Return the page's blocks: normalized bbox, relative size, kind and text."""
|
|
18
|
-
document = fitz.open(pdf_path)
|
|
19
|
-
page = document[page_number - 1]
|
|
20
|
-
width, height = page.rect.width, page.rect.height
|
|
21
|
-
data = page.get_text("dict")
|
|
22
|
-
|
|
23
|
-
items = []
|
|
24
|
-
for block in data.get("blocks", []):
|
|
25
|
-
if block.get("type", 0) != 0:
|
|
26
|
-
continue
|
|
27
|
-
lines = block.get("lines", [])
|
|
28
|
-
sizes = []
|
|
29
|
-
text = []
|
|
30
|
-
for line in lines:
|
|
31
|
-
for span in line.get("spans", []):
|
|
32
|
-
sizes.append(span.get("size", 0))
|
|
33
|
-
text.append(span.get("text", ""))
|
|
34
|
-
content = " ".join(part.strip() for part in text if part.strip())
|
|
35
|
-
if content == "":
|
|
36
|
-
continue
|
|
37
|
-
bbox = block["bbox"]
|
|
38
|
-
items.append({
|
|
39
|
-
"bbox": [
|
|
40
|
-
bbox[0] / width,
|
|
41
|
-
bbox[1] / height,
|
|
42
|
-
(bbox[2] - bbox[0]) / width,
|
|
43
|
-
(bbox[3] - bbox[1]) / height,
|
|
44
|
-
],
|
|
45
|
-
"size": max(sizes) if sizes else 0,
|
|
46
|
-
"text": content[:400],
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
def center(item):
|
|
50
|
-
return (item["bbox"][0] + item["bbox"][2] / 2, item["bbox"][1] + item["bbox"][3] / 2)
|
|
51
|
-
|
|
52
|
-
# Superscripts/subscripts often arrive as their own tiny block. Merge them into
|
|
53
|
-
# the nearest real block instead of dropping them, so the paragraph box covers
|
|
54
|
-
# the small glyph too.
|
|
55
|
-
tiny = [item for item in items if item["bbox"][3] < 0.028 and len(item["text"]) <= 8]
|
|
56
|
-
keep = [item for item in items if item not in tiny]
|
|
57
|
-
for small in tiny:
|
|
58
|
-
sx, sy = center(small)
|
|
59
|
-
nearest = None
|
|
60
|
-
best = 0.035
|
|
61
|
-
for candidate in keep:
|
|
62
|
-
cx, cy = center(candidate)
|
|
63
|
-
distance = abs(sy - cy)
|
|
64
|
-
if distance < best:
|
|
65
|
-
best = distance
|
|
66
|
-
nearest = candidate
|
|
67
|
-
if nearest is None:
|
|
68
|
-
keep.append(small)
|
|
69
|
-
continue
|
|
70
|
-
x0 = min(nearest["bbox"][0], small["bbox"][0])
|
|
71
|
-
y0 = min(nearest["bbox"][1], small["bbox"][1])
|
|
72
|
-
x1 = max(nearest["bbox"][0] + nearest["bbox"][2], small["bbox"][0] + small["bbox"][2])
|
|
73
|
-
y1 = max(nearest["bbox"][1] + nearest["bbox"][3], small["bbox"][1] + small["bbox"][3])
|
|
74
|
-
nearest["bbox"] = [x0, y0, x1 - x0, y1 - y0]
|
|
75
|
-
nearest["text"] = f"{nearest['text']} {small['text']}".strip()[:400]
|
|
76
|
-
items = keep
|
|
77
|
-
|
|
78
|
-
ordered = sorted(item["size"] for item in items)
|
|
79
|
-
median = ordered[len(ordered) // 2] if ordered else 1.0
|
|
80
|
-
for index, item in enumerate(items):
|
|
81
|
-
item["id"] = f"b-{page_number:04d}-{index + 1:02d}"
|
|
82
|
-
item["page"] = page_number
|
|
83
|
-
left, top, width, height = item["bbox"]
|
|
84
|
-
if top < 0.09 and width < 0.7:
|
|
85
|
-
item["kind"] = "heading"
|
|
86
|
-
elif width < 0.62 and left > 0.12 and left + width < 0.9 and height < 0.05:
|
|
87
|
-
item["kind"] = "formula"
|
|
88
|
-
else:
|
|
89
|
-
item["kind"] = "paragraph"
|
|
90
|
-
item["size"] = item["size"] / median if median > 0 else 1.0
|
|
91
|
-
|
|
92
|
-
# Page numbers / footers: tiny blocks in the bottom margin carry no paragraph
|
|
93
|
-
# content and were being misread as formulas.
|
|
94
|
-
items = [
|
|
95
|
-
item for item in items
|
|
96
|
-
if not (item["bbox"][1] > 0.9 and item["bbox"][3] < 0.03 and item["bbox"][2] < 0.12)
|
|
97
|
-
]
|
|
98
|
-
|
|
99
|
-
return items
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def main():
|
|
103
|
-
sys.stdout.reconfigure(encoding="utf-8")
|
|
104
|
-
pdf_path = sys.argv[1]
|
|
105
|
-
page_number = int(sys.argv[2])
|
|
106
|
-
print(json.dumps(blocks_for_page(pdf_path, page_number), ensure_ascii=False))
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if __name__ == "__main__":
|
|
110
|
-
main()
|
|
1
|
+
"""One PDF page's layout blocks as JSON (PyMuPDF).
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
python pdf_blocks.py <input.pdf> <page-index-1-based>
|
|
5
|
+
|
|
6
|
+
The parsing is also importable: `blocks_for_page(path, number)` returns the same
|
|
7
|
+
list, which is what the paragraph realigner uses instead of spawning this script
|
|
8
|
+
(no pipes, no second interpreter).
|
|
9
|
+
"""
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
import fitz
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def blocks_for_page(pdf_path, page_number):
|
|
17
|
+
"""Return the page's blocks: normalized bbox, relative size, kind and text."""
|
|
18
|
+
document = fitz.open(pdf_path)
|
|
19
|
+
page = document[page_number - 1]
|
|
20
|
+
width, height = page.rect.width, page.rect.height
|
|
21
|
+
data = page.get_text("dict")
|
|
22
|
+
|
|
23
|
+
items = []
|
|
24
|
+
for block in data.get("blocks", []):
|
|
25
|
+
if block.get("type", 0) != 0:
|
|
26
|
+
continue
|
|
27
|
+
lines = block.get("lines", [])
|
|
28
|
+
sizes = []
|
|
29
|
+
text = []
|
|
30
|
+
for line in lines:
|
|
31
|
+
for span in line.get("spans", []):
|
|
32
|
+
sizes.append(span.get("size", 0))
|
|
33
|
+
text.append(span.get("text", ""))
|
|
34
|
+
content = " ".join(part.strip() for part in text if part.strip())
|
|
35
|
+
if content == "":
|
|
36
|
+
continue
|
|
37
|
+
bbox = block["bbox"]
|
|
38
|
+
items.append({
|
|
39
|
+
"bbox": [
|
|
40
|
+
bbox[0] / width,
|
|
41
|
+
bbox[1] / height,
|
|
42
|
+
(bbox[2] - bbox[0]) / width,
|
|
43
|
+
(bbox[3] - bbox[1]) / height,
|
|
44
|
+
],
|
|
45
|
+
"size": max(sizes) if sizes else 0,
|
|
46
|
+
"text": content[:400],
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
def center(item):
|
|
50
|
+
return (item["bbox"][0] + item["bbox"][2] / 2, item["bbox"][1] + item["bbox"][3] / 2)
|
|
51
|
+
|
|
52
|
+
# Superscripts/subscripts often arrive as their own tiny block. Merge them into
|
|
53
|
+
# the nearest real block instead of dropping them, so the paragraph box covers
|
|
54
|
+
# the small glyph too.
|
|
55
|
+
tiny = [item for item in items if item["bbox"][3] < 0.028 and len(item["text"]) <= 8]
|
|
56
|
+
keep = [item for item in items if item not in tiny]
|
|
57
|
+
for small in tiny:
|
|
58
|
+
sx, sy = center(small)
|
|
59
|
+
nearest = None
|
|
60
|
+
best = 0.035
|
|
61
|
+
for candidate in keep:
|
|
62
|
+
cx, cy = center(candidate)
|
|
63
|
+
distance = abs(sy - cy)
|
|
64
|
+
if distance < best:
|
|
65
|
+
best = distance
|
|
66
|
+
nearest = candidate
|
|
67
|
+
if nearest is None:
|
|
68
|
+
keep.append(small)
|
|
69
|
+
continue
|
|
70
|
+
x0 = min(nearest["bbox"][0], small["bbox"][0])
|
|
71
|
+
y0 = min(nearest["bbox"][1], small["bbox"][1])
|
|
72
|
+
x1 = max(nearest["bbox"][0] + nearest["bbox"][2], small["bbox"][0] + small["bbox"][2])
|
|
73
|
+
y1 = max(nearest["bbox"][1] + nearest["bbox"][3], small["bbox"][1] + small["bbox"][3])
|
|
74
|
+
nearest["bbox"] = [x0, y0, x1 - x0, y1 - y0]
|
|
75
|
+
nearest["text"] = f"{nearest['text']} {small['text']}".strip()[:400]
|
|
76
|
+
items = keep
|
|
77
|
+
|
|
78
|
+
ordered = sorted(item["size"] for item in items)
|
|
79
|
+
median = ordered[len(ordered) // 2] if ordered else 1.0
|
|
80
|
+
for index, item in enumerate(items):
|
|
81
|
+
item["id"] = f"b-{page_number:04d}-{index + 1:02d}"
|
|
82
|
+
item["page"] = page_number
|
|
83
|
+
left, top, width, height = item["bbox"]
|
|
84
|
+
if top < 0.09 and width < 0.7:
|
|
85
|
+
item["kind"] = "heading"
|
|
86
|
+
elif width < 0.62 and left > 0.12 and left + width < 0.9 and height < 0.05:
|
|
87
|
+
item["kind"] = "formula"
|
|
88
|
+
else:
|
|
89
|
+
item["kind"] = "paragraph"
|
|
90
|
+
item["size"] = item["size"] / median if median > 0 else 1.0
|
|
91
|
+
|
|
92
|
+
# Page numbers / footers: tiny blocks in the bottom margin carry no paragraph
|
|
93
|
+
# content and were being misread as formulas.
|
|
94
|
+
items = [
|
|
95
|
+
item for item in items
|
|
96
|
+
if not (item["bbox"][1] > 0.9 and item["bbox"][3] < 0.03 and item["bbox"][2] < 0.12)
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
return items
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def main():
|
|
103
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
104
|
+
pdf_path = sys.argv[1]
|
|
105
|
+
page_number = int(sys.argv[2])
|
|
106
|
+
print(json.dumps(blocks_for_page(pdf_path, page_number), ensure_ascii=False))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if __name__ == "__main__":
|
|
110
|
+
main()
|
package/tools/pdf_fontsize.py
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
"""Print the median body font size (pt) of a PDF."""
|
|
2
|
-
import json
|
|
3
|
-
import sys
|
|
4
|
-
|
|
5
|
-
import fitz
|
|
6
|
-
|
|
7
|
-
document = fitz.open(sys.argv[1])
|
|
8
|
-
sizes = []
|
|
9
|
-
for page in document:
|
|
10
|
-
for block in page.get_text("dict").get("blocks", []):
|
|
11
|
-
if block.get("type", 0) != 0:
|
|
12
|
-
continue
|
|
13
|
-
for line in block.get("lines", []):
|
|
14
|
-
for span in line.get("spans", []):
|
|
15
|
-
value = span.get("size", 0)
|
|
16
|
-
if value > 0:
|
|
17
|
-
sizes.append(value)
|
|
18
|
-
sizes.sort()
|
|
19
|
-
median = sizes[len(sizes) // 2] if sizes else 11.0
|
|
20
|
-
print(json.dumps(median))
|
|
1
|
+
"""Print the median body font size (pt) of a PDF."""
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
import fitz
|
|
6
|
+
|
|
7
|
+
document = fitz.open(sys.argv[1])
|
|
8
|
+
sizes = []
|
|
9
|
+
for page in document:
|
|
10
|
+
for block in page.get_text("dict").get("blocks", []):
|
|
11
|
+
if block.get("type", 0) != 0:
|
|
12
|
+
continue
|
|
13
|
+
for line in block.get("lines", []):
|
|
14
|
+
for span in line.get("spans", []):
|
|
15
|
+
value = span.get("size", 0)
|
|
16
|
+
if value > 0:
|
|
17
|
+
sizes.append(value)
|
|
18
|
+
sizes.sort()
|
|
19
|
+
median = sizes[len(sizes) // 2] if sizes else 11.0
|
|
20
|
+
print(json.dumps(median))
|
package/tools/pdf_merge.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
"""Merge single-page PDFs into one.
|
|
2
|
-
|
|
3
|
-
Usage: python pdf_merge.py <output.pdf> <page-1.pdf> <page-2.pdf> ...
|
|
4
|
-
"""
|
|
5
|
-
import sys
|
|
6
|
-
|
|
7
|
-
import fitz
|
|
8
|
-
|
|
9
|
-
output = sys.argv[1]
|
|
10
|
-
merged = fitz.open()
|
|
11
|
-
for path in sys.argv[2:]:
|
|
12
|
-
page_doc = fitz.open(path)
|
|
13
|
-
merged.insert_pdf(page_doc)
|
|
14
|
-
merged.save(output)
|
|
15
|
-
print(output)
|
|
1
|
+
"""Merge single-page PDFs into one.
|
|
2
|
+
|
|
3
|
+
Usage: python pdf_merge.py <output.pdf> <page-1.pdf> <page-2.pdf> ...
|
|
4
|
+
"""
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
import fitz
|
|
8
|
+
|
|
9
|
+
output = sys.argv[1]
|
|
10
|
+
merged = fitz.open()
|
|
11
|
+
for path in sys.argv[2:]:
|
|
12
|
+
page_doc = fitz.open(path)
|
|
13
|
+
merged.insert_pdf(page_doc)
|
|
14
|
+
merged.save(output)
|
|
15
|
+
print(output)
|
package/tools/pdf_pagesizes.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"""Print page sizes of a PDF as JSON [[width_pt, height_pt], ...]."""
|
|
2
|
-
import json
|
|
3
|
-
import sys
|
|
4
|
-
|
|
5
|
-
import fitz
|
|
6
|
-
|
|
7
|
-
document = fitz.open(sys.argv[1])
|
|
8
|
-
print(json.dumps([[page.rect.width, page.rect.height] for page in document]))
|
|
1
|
+
"""Print page sizes of a PDF as JSON [[width_pt, height_pt], ...]."""
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
import fitz
|
|
6
|
+
|
|
7
|
+
document = fitz.open(sys.argv[1])
|
|
8
|
+
print(json.dumps([[page.rect.width, page.rect.height] for page in document]))
|
package/tools/pdf_raster.py
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
"""Rasterize every page of a PDF to PNG files with PyMuPDF.
|
|
2
|
-
|
|
3
|
-
Usage: python pdf_raster.py <input.pdf> <output-dir> [dpi]
|
|
4
|
-
|
|
5
|
-
The translation job uses this so the Host owns the image pipeline: leaving the
|
|
6
|
-
reader no longer interrupts a running chapter translation.
|
|
7
|
-
"""
|
|
8
|
-
import os
|
|
9
|
-
import sys
|
|
10
|
-
|
|
11
|
-
import fitz
|
|
12
|
-
|
|
13
|
-
pdf_path, out_dir = sys.argv[1], sys.argv[2]
|
|
14
|
-
dpi = int(sys.argv[3]) if len(sys.argv) > 3 else 140
|
|
15
|
-
|
|
16
|
-
os.makedirs(out_dir, exist_ok=True)
|
|
17
|
-
document = fitz.open(pdf_path)
|
|
18
|
-
for index, page in enumerate(document):
|
|
19
|
-
pixmap = page.get_pixmap(dpi=dpi)
|
|
20
|
-
pixmap.save(os.path.join(out_dir, f"p{index + 1:04d}.png"))
|
|
21
|
-
print(len(document))
|
|
1
|
+
"""Rasterize every page of a PDF to PNG files with PyMuPDF.
|
|
2
|
+
|
|
3
|
+
Usage: python pdf_raster.py <input.pdf> <output-dir> [dpi]
|
|
4
|
+
|
|
5
|
+
The translation job uses this so the Host owns the image pipeline: leaving the
|
|
6
|
+
reader no longer interrupts a running chapter translation.
|
|
7
|
+
"""
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
import fitz
|
|
12
|
+
|
|
13
|
+
pdf_path, out_dir = sys.argv[1], sys.argv[2]
|
|
14
|
+
dpi = int(sys.argv[3]) if len(sys.argv) > 3 else 140
|
|
15
|
+
|
|
16
|
+
os.makedirs(out_dir, exist_ok=True)
|
|
17
|
+
document = fitz.open(pdf_path)
|
|
18
|
+
for index, page in enumerate(document):
|
|
19
|
+
pixmap = page.get_pixmap(dpi=dpi)
|
|
20
|
+
pixmap.save(os.path.join(out_dir, f"p{index + 1:04d}.png"))
|
|
21
|
+
print(len(document))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Whole PDF text as one markdown file (PyMuPDF).
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
python pdf_text.py <input.pdf>
|
|
5
|
+
|
|
6
|
+
Prints JSON:
|
|
7
|
+
{"pages": [{"page": 1, "text": "..."}, ...], "chars": 12345, "pageCount": 42}
|
|
8
|
+
|
|
9
|
+
The Host keeps the result in `.pdfdesk/cache/<book>/<chapter>/text.md` so the
|
|
10
|
+
hidden base session can read a chapter once instead of re-reading the PDF for
|
|
11
|
+
every mode.
|
|
12
|
+
"""
|
|
13
|
+
import json
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
import fitz
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def whole_text(pdf_path):
|
|
20
|
+
document = fitz.open(pdf_path)
|
|
21
|
+
pages = []
|
|
22
|
+
chars = 0
|
|
23
|
+
for index, page in enumerate(document, start=1):
|
|
24
|
+
text = page.get_text("text").strip()
|
|
25
|
+
chars += len(text)
|
|
26
|
+
pages.append({"page": index, "text": text})
|
|
27
|
+
return {"pages": pages, "chars": chars, "pageCount": len(pages)}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
if len(sys.argv) < 2:
|
|
32
|
+
print("usage: pdf_text.py <input.pdf>", file=sys.stderr)
|
|
33
|
+
sys.exit(2)
|
|
34
|
+
print(json.dumps(whole_text(sys.argv[1]), ensure_ascii=False))
|