pdfsieve 0.1.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.
- pdfsieve-0.1.0/LICENSE +202 -0
- pdfsieve-0.1.0/NOTICE +7 -0
- pdfsieve-0.1.0/PKG-INFO +391 -0
- pdfsieve-0.1.0/README.md +361 -0
- pdfsieve-0.1.0/THIRD_PARTY_NOTICES.md +38 -0
- pdfsieve-0.1.0/pyproject.toml +45 -0
- pdfsieve-0.1.0/setup.cfg +4 -0
- pdfsieve-0.1.0/src/pdfsieve/__init__.py +25 -0
- pdfsieve-0.1.0/src/pdfsieve/__main__.py +107 -0
- pdfsieve-0.1.0/src/pdfsieve/model.py +228 -0
- pdfsieve-0.1.0/src/pdfsieve/py.typed +0 -0
- pdfsieve-0.1.0/src/pdfsieve/reader.py +339 -0
- pdfsieve-0.1.0/src/pdfsieve/segments.py +42 -0
- pdfsieve-0.1.0/src/pdfsieve/words.py +107 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/PKG-INFO +391 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/SOURCES.txt +19 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/dependency_links.txt +1 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/entry_points.txt +2 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/requires.txt +10 -0
- pdfsieve-0.1.0/src/pdfsieve.egg-info/top_level.txt +1 -0
- pdfsieve-0.1.0/tests/test_reader.py +247 -0
pdfsieve-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
pdfsieve-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
pdfsieve
|
|
2
|
+
Copyright 2026 Vaibhav Rawat (AdRaw App Studio)
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
It reads PDF pages: words, characters, lines, shapes and images with coordinates.
|
|
6
|
+
|
|
7
|
+
Originally developed as part of docextract by AdRaw App Studio.
|
pdfsieve-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pdfsieve
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read characters, words, lines, shapes and images from PDF pages with exact coordinates. Pure Python, permissively licensed (pdfminer.six underneath).
|
|
5
|
+
Author: Vaibhav Rawat (AdRaw App Studio)
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/Vibhu1712/pdfsieve
|
|
8
|
+
Project-URL: Issues, https://github.com/Vibhu1712/pdfsieve/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/Vibhu1712/pdfsieve/blob/main/CHANGELOG.md
|
|
10
|
+
Keywords: pdf,text extraction,word coordinates,pdfminer,table borders,pdf parsing,document ai
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Topic :: Text Processing :: General
|
|
17
|
+
Classifier: Topic :: Office/Business
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
License-File: NOTICE
|
|
23
|
+
License-File: THIRD_PARTY_NOTICES.md
|
|
24
|
+
Requires-Dist: pdfminer.six>=20240706
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
27
|
+
Requires-Dist: reportlab>=4; python_version >= "3.10" and extra == "test"
|
|
28
|
+
Requires-Dist: reportlab<=4.5.0,>=4; python_version < "3.10" and extra == "test"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# pdfsieve
|
|
32
|
+
|
|
33
|
+
**Read words, characters, lines, shapes and images from PDF pages, with exact coordinates.**
|
|
34
|
+
Pure Python, Apache-2.0 licensed, built on [pdfminer.six](https://github.com/pdfminer/pdfminer.six).
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
import pdfsieve
|
|
38
|
+
|
|
39
|
+
with pdfsieve.open("payslip.pdf") as doc:
|
|
40
|
+
for w in doc.page(0).words():
|
|
41
|
+
print(f"{w.x0:6.1f} {w.y0:6.1f} {w.text}")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+

|
|
45
|
+
|
|
46
|
+
## Why pdfsieve
|
|
47
|
+
|
|
48
|
+
- **Coordinates you can use directly.** Everything is in PDF points with the origin at the
|
|
49
|
+
**top-left** of the visible page, y pointing down, with the page's rotation and crop already
|
|
50
|
+
applied. A box scales straight onto a rendered image: multiply by your zoom and draw.
|
|
51
|
+
- **Words that match what a viewer shows.** Characters are joined into words with rules tuned on
|
|
52
|
+
real payslips, invoices and bank statements: justified text, tight table cells, fake-bold text
|
|
53
|
+
(letters printed twice), ligatures (`fi` → `fi`), and sideways text.
|
|
54
|
+
- **More than text.** Table borders as straight segments, filled and stroked shapes with their
|
|
55
|
+
colours, embedded images with their placement, fonts, and per-page signals that tell a digital
|
|
56
|
+
page from a scan or from text drawn as outlines.
|
|
57
|
+
- **No native code.** Only pure-Python dependencies, so it installs and runs where policies block
|
|
58
|
+
native PDF DLLs (Windows Application Control, locked-down servers, restricted sandboxes).
|
|
59
|
+
- **Permissive licence.** Apache-2.0, on top of MIT-licensed pdfminer.six. Use it in commercial,
|
|
60
|
+
closed-source products.
|
|
61
|
+
|
|
62
|
+
Need a picture of the page too, for OCR or for drawing boxes on screen? See the companion
|
|
63
|
+
package **[pdfpainter](https://github.com/Vibhu1712/pdfpainter)**, which paints
|
|
64
|
+
pdfsieve pages with Pillow alone.
|
|
65
|
+
|
|
66
|
+
## Contents
|
|
67
|
+
|
|
68
|
+
- [Install](#install)
|
|
69
|
+
- [Quick start](#quick-start)
|
|
70
|
+
- [Command line](#command-line)
|
|
71
|
+
- [Coordinates](#coordinates)
|
|
72
|
+
- [API reference](#api-reference)
|
|
73
|
+
- [Recipes](#recipes)
|
|
74
|
+
- [How words are formed](#how-words-are-formed)
|
|
75
|
+
- [Accuracy and speed](#accuracy-and-speed)
|
|
76
|
+
- [Limitations](#limitations)
|
|
77
|
+
- [How it compares](#how-it-compares)
|
|
78
|
+
- [Development](#development)
|
|
79
|
+
- [Licence](#licence)
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install pdfsieve
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Python 3.9 or newer. The only dependency is `pdfminer.six`.
|
|
88
|
+
|
|
89
|
+
## Quick start
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import pdfsieve
|
|
93
|
+
|
|
94
|
+
with pdfsieve.open("invoice.pdf") as doc: # a path, the PDF's bytes, or a binary file
|
|
95
|
+
print(doc.page_count, doc.metadata().get("title"))
|
|
96
|
+
|
|
97
|
+
page = doc.page(0) # 0-based; doc[-1] is the last page
|
|
98
|
+
print(page.width, page.height) # points, after rotation
|
|
99
|
+
|
|
100
|
+
print(page.text()) # plain text, line by line
|
|
101
|
+
|
|
102
|
+
for w in page.words(): # Word(x0, y0, x1, y1, text)
|
|
103
|
+
print(w.text, w.x0, w.y0, w.x1, w.y1)
|
|
104
|
+
|
|
105
|
+
for line in page.lines(): # words grouped into lines
|
|
106
|
+
print(" | ".join(w.text for w in line))
|
|
107
|
+
|
|
108
|
+
for seg in page.vertical_lines(): # table borders: Segment(x, top, bottom)
|
|
109
|
+
print("vertical rule at x =", seg.pos)
|
|
110
|
+
|
|
111
|
+
for pic in page.pictures: # embedded images
|
|
112
|
+
print("image at", pic.bbox)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Iterate over all pages with `for page in doc:`. Pages are read when first asked for and then
|
|
116
|
+
remembered; call `doc.forget()` to free memory on very long documents.
|
|
117
|
+
|
|
118
|
+
## Command line
|
|
119
|
+
|
|
120
|
+
Installing the package adds a `pdfsieve` command (also `python -m pdfsieve`):
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pdfsieve payslip.pdf # plain text of every page (pages separated by form feeds)
|
|
124
|
+
pdfsieve payslip.pdf -p 1,3-5 # only some pages (1-based)
|
|
125
|
+
pdfsieve payslip.pdf -f words # page, x0, y0, x1, y1, text, tab-separated
|
|
126
|
+
pdfsieve payslip.pdf -f json > out.json # lines of words with boxes, plus page facts
|
|
127
|
+
pdfsieve payslip.pdf -f info # page sizes, word/image/shape/curve counts, fonts
|
|
128
|
+
pdfsieve scan.pdf --visible-only # ignore a scan's hidden OCR layer
|
|
129
|
+
pdfsieve locked.pdf --password secret
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`-f info` is a quick way to see why a page has no text:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
File: sample.pdf
|
|
136
|
+
Pages: 1 encrypted: False
|
|
137
|
+
Title: Sample payslip
|
|
138
|
+
Producer: ReportLab PDF Library - (opensource)
|
|
139
|
+
Page 1: 595.3 x 841.9 pt, rotate 0, 53 words, 1 images, 14 shapes, 8 curves, 3 fonts
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Coordinates
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
(0, 0) ──────────────── x ──────────────▶ (width, 0)
|
|
146
|
+
│
|
|
147
|
+
│ ┌─────────────┐
|
|
148
|
+
│ │ (x0, y0) │ a word's box: x0 < x1, y0 < y1
|
|
149
|
+
y │ (x1, y1)
|
|
150
|
+
│ └─────────────┘
|
|
151
|
+
▼
|
|
152
|
+
(0, height)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
- Units are PDF points: 1/72 inch. Multiply by `zoom` (pixels per point) to land on a rendered
|
|
156
|
+
image; at 150 dpi, `zoom = 150 / 72`.
|
|
157
|
+
- The origin is the top-left of the **CropBox** (the part of the page viewers show).
|
|
158
|
+
- A page's `/Rotate` is already applied: `page.width` and `page.height` are the size you see, and
|
|
159
|
+
text on a rotated page comes out with `Glyph.turn` set accordingly.
|
|
160
|
+
- Word boxes span the font's ascent and descent, as other engines report them, so boxes on one line
|
|
161
|
+
share their top and bottom even for words without tall letters.
|
|
162
|
+
|
|
163
|
+
## API reference
|
|
164
|
+
|
|
165
|
+
### Opening
|
|
166
|
+
|
|
167
|
+
| | |
|
|
168
|
+
|---|---|
|
|
169
|
+
| `pdfsieve.open(source, password="")` | Open a PDF. `source` is a path, `bytes`, or a binary file object. Returns a `Document`. |
|
|
170
|
+
| `pdfsieve.Document(source, password="")` | Same as `open`. |
|
|
171
|
+
|
|
172
|
+
Errors: `PasswordError` (also a `PermissionError`) when a password is needed or wrong,
|
|
173
|
+
`EmptyDocumentError` (also a `ValueError`) for a PDF with no pages, `PdfSieveError` (the base
|
|
174
|
+
class) for anything that isn't a readable PDF. A missing file raises the usual `FileNotFoundError`.
|
|
175
|
+
|
|
176
|
+
### `Document`
|
|
177
|
+
|
|
178
|
+
| Member | Meaning |
|
|
179
|
+
|---|---|
|
|
180
|
+
| `page_count`, `len(doc)` | Number of pages. |
|
|
181
|
+
| `page(i)`, `doc[i]` | The `Page` at 0-based index `i` (negative counts from the end). |
|
|
182
|
+
| `for page in doc` | Every page in order. |
|
|
183
|
+
| `size(i)` | `(width, height)` of a page without reading its content. |
|
|
184
|
+
| `metadata()` | Info dictionary with lower-case keys: `title`, `author`, `producer`, `creator`, ... |
|
|
185
|
+
| `encrypted` | True when the file was encrypted (and opened). |
|
|
186
|
+
| `errors` | `{page index: message}` for damaged pages. What was read before the fault is kept. |
|
|
187
|
+
| `forget(i=None)` | Drop remembered content of one page, or all pages. |
|
|
188
|
+
| `close()`, `with ...` | Close the file. A file object you passed in is left open. |
|
|
189
|
+
|
|
190
|
+
### `Page`
|
|
191
|
+
|
|
192
|
+
| Member | Meaning |
|
|
193
|
+
|---|---|
|
|
194
|
+
| `index` | 0-based page number. |
|
|
195
|
+
| `width`, `height` | Visible size in points, after rotation. |
|
|
196
|
+
| `rotation` | The page's `/Rotate`: 0, 90, 180 or 270 (already applied). |
|
|
197
|
+
| `words(visible_only=False)` | List of `Word`, line by line. `visible_only` skips invisible text such as a scan's OCR layer. |
|
|
198
|
+
| `lines(visible_only=False)` | List of lines, each a list of `Word`. |
|
|
199
|
+
| `text(visible_only=False)` | Plain text: words joined by spaces, lines by newlines. |
|
|
200
|
+
| `vertical_lines(min_len=4)` | `Segment(x, top, bottom)` for vertical strokes and rectangle edges. |
|
|
201
|
+
| `horizontal_lines(min_len=4)` | `Segment(y, left, right)` for horizontal strokes and rectangle edges. |
|
|
202
|
+
| `glyphs` | Every character as a `Glyph`, in drawing order. |
|
|
203
|
+
| `items` | `Shape` and `Picture` objects in drawing order. |
|
|
204
|
+
| `shapes`, `pictures` | Only the shapes, or only the pictures. |
|
|
205
|
+
| `fonts` | Set of font names used on the page. |
|
|
206
|
+
| `images` | Number of images drawn. |
|
|
207
|
+
| `curves` | Number of Bézier curve segments. Hundreds with no text usually means text drawn as outlines. |
|
|
208
|
+
| `undefined` | Characters with no Unicode mapping (they appear as `"�"`). |
|
|
209
|
+
|
|
210
|
+
### Data types
|
|
211
|
+
|
|
212
|
+
**`Word(x0, y0, x1, y1, text)`**, a `NamedTuple`, so `x0, y0, x1, y1, text = word` works. Extra
|
|
213
|
+
properties: `cx`, `cy` (centre), `width`, `height`.
|
|
214
|
+
|
|
215
|
+
**`Glyph`**, one character:
|
|
216
|
+
|
|
217
|
+
| Field | Meaning |
|
|
218
|
+
|---|---|
|
|
219
|
+
| `x0, y0, x1, y1` | Box in page space. |
|
|
220
|
+
| `text` | The character (ligatures stay as one character here). |
|
|
221
|
+
| `size` | Font size on the page, in points. |
|
|
222
|
+
| `font` | PDF font name, e.g. `ABCDEF+Arial-BoldMT`. |
|
|
223
|
+
| `color` | Fill colour `(r, g, b)` 0–255, or `None` when it can't be known. |
|
|
224
|
+
| `visible` | False for invisible text (render modes 3 and 7). |
|
|
225
|
+
| `upright`, `turn` | Writing direction: `turn` is 0, 90 (reads upwards), 180, or 270 (reads downwards). |
|
|
226
|
+
| `base` | Baseline y. |
|
|
227
|
+
| `run` | Which text-showing operation drew it. Characters of one table cell usually share a run. |
|
|
228
|
+
|
|
229
|
+
**`Shape`**, one painted path: `subpaths` (a list of `(points, closed)`, where points are
|
|
230
|
+
`("m", x, y)`, `("l", x, y)` and `("c", x1, y1, x2, y2, x3, y3)`), `fill` and `stroke` colours
|
|
231
|
+
(`None` when not filled or stroked), `width` (stroke width on the page), and `evenodd` (fill rule).
|
|
232
|
+
|
|
233
|
+
**`Picture`**, one embedded image: `stream` (the pdfminer stream with the image data), `matrix`
|
|
234
|
+
(maps the image's unit square onto the page), `bbox` (its box on the page), and `color` (the paint
|
|
235
|
+
colour of 1-bit stencil masks).
|
|
236
|
+
|
|
237
|
+
**`Segment(pos, a0, a1)`**: `pos` is x for vertical segments and y for horizontal ones; `a0` and
|
|
238
|
+
`a1` are the start and end along the segment; `length` is `a1 - a0`.
|
|
239
|
+
|
|
240
|
+
### Lower-level functions
|
|
241
|
+
|
|
242
|
+
| Function | Meaning |
|
|
243
|
+
|---|---|
|
|
244
|
+
| `words_from(glyphs)` | Join any list of `Glyph` into `Word`s, e.g. only the glyphs inside a region. |
|
|
245
|
+
| `lines_from(glyphs)` | The same, grouped into lines. |
|
|
246
|
+
| `vertical_segments(page, min_len)`, `horizontal_segments(page, min_len)` | Same as the `Page` methods. |
|
|
247
|
+
| `to_rgb(color, colorspace)` | Convert a PDF colour to `(r, g, b)`. |
|
|
248
|
+
| `Recorder` | The pdfminer device that does the recording, for building your own pipeline. |
|
|
249
|
+
|
|
250
|
+
## Recipes
|
|
251
|
+
|
|
252
|
+
Runnable versions live in [`examples/`](examples). Make the sample PDF they use with
|
|
253
|
+
`python examples/make_sample_pdf.py` (needs `pip install reportlab`).
|
|
254
|
+
|
|
255
|
+
### Words inside a region
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
def words_in(page, box):
|
|
259
|
+
x0, y0, x1, y1 = box
|
|
260
|
+
return [w for w in page.words() if x0 <= w.cx <= x1 and y0 <= w.cy <= y1]
|
|
261
|
+
|
|
262
|
+
net_pay_row = words_in(page, (60, 360, 535, 384)) # "Net pay 66,360.00" in the sample
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### The value to the right of a label
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
def value_after(page, label):
|
|
269
|
+
words = page.words()
|
|
270
|
+
for i, w in enumerate(words):
|
|
271
|
+
if w.text == label:
|
|
272
|
+
right = [v for v in words if abs(v.cy - w.cy) < w.height / 2 and v.x0 > w.x1]
|
|
273
|
+
return " ".join(v.text for v in sorted(right, key=lambda v: v.x0))
|
|
274
|
+
|
|
275
|
+
value_after(page, "Department") # ': Finance'
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### A ruled table into rows and columns
|
|
279
|
+
|
|
280
|
+
[`examples/table_cells.py`](examples/table_cells.py) builds the grid from the table's border lines
|
|
281
|
+
and drops each word into the cell its centre falls in:
|
|
282
|
+
|
|
283
|
+
```
|
|
284
|
+
Component | Earnings | Deductions
|
|
285
|
+
Basic pay | 52,000.00 |
|
|
286
|
+
House rent allowance | 20,800.00 |
|
|
287
|
+
Provident fund | | 6,240.00
|
|
288
|
+
Professional tax | | 200.00
|
|
289
|
+
Net pay | 66,360.00 |
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### Does this page need OCR?
|
|
293
|
+
|
|
294
|
+
[`examples/page_kinds.py`](examples/page_kinds.py) tells the cases apart:
|
|
295
|
+
|
|
296
|
+
```python
|
|
297
|
+
words = page.words(visible_only=True)
|
|
298
|
+
if words: "digital text"
|
|
299
|
+
elif page.words(): "scan with a hidden OCR layer"
|
|
300
|
+
elif page.curves >= 150: "text drawn as outlines ('Microsoft Print to PDF'): OCR it"
|
|
301
|
+
elif page.images: "scanned image: OCR it"
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
To OCR without a native PDF engine, paint the page with
|
|
305
|
+
[pdfpainter](https://github.com/Vibhu1712/pdfpainter) and pass the image to Tesseract or
|
|
306
|
+
RapidOCR. The boxes come back in the same coordinates as pdfsieve's.
|
|
307
|
+
|
|
308
|
+
### All words to CSV
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
python examples/words_to_csv.py statement.pdf words.csv
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### From memory or the network
|
|
315
|
+
|
|
316
|
+
```python
|
|
317
|
+
data = requests.get(url).content
|
|
318
|
+
with pdfsieve.open(data) as doc:
|
|
319
|
+
...
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## How words are formed
|
|
323
|
+
|
|
324
|
+
1. Characters are grouped into lines: a character joins a line when its vertical centre is within
|
|
325
|
+
35% of the letter size of the line's last character.
|
|
326
|
+
2. Within a line, a space character, or a gap wider than **12%** of the letter size, ends a word.
|
|
327
|
+
Characters from separate text operations (usually separate table cells) are split at **8%**.
|
|
328
|
+
Justified text squeezes real spaces down to about 16%, so these thresholds keep `52,000.00`
|
|
329
|
+
together while still separating cells that sit close together.
|
|
330
|
+
3. A letter printed twice at nearly the same spot (a common way of faking bold) is read once.
|
|
331
|
+
4. Ligatures `ff fi fl ffi ffl ſt st` are expanded.
|
|
332
|
+
5. Sideways text is grouped in its own reading direction and listed after the upright text.
|
|
333
|
+
|
|
334
|
+
The thresholds are module constants (`pdfsieve.words.GAP`, `RUN_GAP`, `LINE_TOL`) if a document
|
|
335
|
+
family needs different ones.
|
|
336
|
+
|
|
337
|
+
## Accuracy and speed
|
|
338
|
+
|
|
339
|
+
Checked against PDFium (the engine in Chrome) on mixed real-world PDFs: every character PDFium
|
|
340
|
+
reports falls inside one of pdfsieve's word boxes (2,790 of 2,790 on the reference set), and page
|
|
341
|
+
sizes agree exactly, rotated pages included.
|
|
342
|
+
|
|
343
|
+
pdfsieve is pure Python, so it is slower than native engines. In testing, reading a page and
|
|
344
|
+
grouping its words took about **70–120 ms**, depending on how much is on the page. That
|
|
345
|
+
is fine for forms, payslips and statements; for bulk processing of thousands of pages, use several
|
|
346
|
+
processes.
|
|
347
|
+
|
|
348
|
+
## Limitations
|
|
349
|
+
|
|
350
|
+
- **No OCR.** Pages without a text layer give no words; see the recipe above.
|
|
351
|
+
- **Fonts without a Unicode mapping** give `"�"` characters (counted in `page.undefined`).
|
|
352
|
+
Some bank and insurance PDFs are made this way on purpose; OCR is the way round it.
|
|
353
|
+
- **Annotations and form fields** (filled-in AcroForm values) aren't read, only the page content.
|
|
354
|
+
- **Right-to-left scripts** come out in drawing order, which may not be reading order.
|
|
355
|
+
- **Clipping** isn't applied: text or shapes hidden by a clipping path are still reported.
|
|
356
|
+
|
|
357
|
+
## How it compares
|
|
358
|
+
|
|
359
|
+
| | pdfsieve | pdfminer.six | pdfplumber | PyMuPDF | pypdfium2 |
|
|
360
|
+
|---|---|---|---|---|---|
|
|
361
|
+
| Licence | Apache-2.0 | MIT | MIT | AGPL or commercial | Apache/BSD |
|
|
362
|
+
| Native code | none | none | none | yes (MuPDF) | yes (PDFium) |
|
|
363
|
+
| Top-left coordinates | yes | no (bottom-left) | yes | yes | no (bottom-left) |
|
|
364
|
+
| Words with boxes | yes | via layout analysis | yes | yes | characters only |
|
|
365
|
+
| Shapes, images with placement | yes | yes | yes | yes | partly |
|
|
366
|
+
| Renders pages | with pdfpainter | no | via pypdfium2 | yes | yes |
|
|
367
|
+
| Speed | moderate | moderate | moderate | fast | fast |
|
|
368
|
+
|
|
369
|
+
Pick pdfsieve when you want permissive licensing, no native dependencies, and coordinates and
|
|
370
|
+
words that are ready to use. If native code is fine and you need maximum speed, PyMuPDF (mind its
|
|
371
|
+
licence) or pypdfium2 will be faster.
|
|
372
|
+
|
|
373
|
+
## Development
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
git clone https://github.com/Vibhu1712/pdfsieve
|
|
377
|
+
cd pdfsieve
|
|
378
|
+
pip install -e ".[test]"
|
|
379
|
+
pytest
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
Tests generate their sample PDFs with reportlab at run time, so the repository holds no binary
|
|
383
|
+
fixtures.
|
|
384
|
+
|
|
385
|
+
## Licence
|
|
386
|
+
|
|
387
|
+
Apache License 2.0; see [LICENSE](LICENSE). Copyright 2026 Vaibhav Rawat (AdRaw App Studio).
|
|
388
|
+
pdfsieve uses pdfminer.six (MIT); see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
|
389
|
+
|
|
390
|
+
pdfsieve began as the PDF reader inside **docextract by AdRaw App Studio**, an offline
|
|
391
|
+
PDF-to-Excel extraction tool, and is published so others can use it too.
|