opencontracting-nutcracker 0.0.1__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.
- opencontracting_nutcracker-0.0.1/LICENSE +29 -0
- opencontracting_nutcracker-0.0.1/MANIFEST.in +8 -0
- opencontracting_nutcracker-0.0.1/PKG-INFO +41 -0
- opencontracting_nutcracker-0.0.1/README.rst +14 -0
- opencontracting_nutcracker-0.0.1/docs/Makefile +20 -0
- opencontracting_nutcracker-0.0.1/docs/api/index.rst +10 -0
- opencontracting_nutcracker-0.0.1/docs/changelog.rst +13 -0
- opencontracting_nutcracker-0.0.1/docs/conf.py +50 -0
- opencontracting_nutcracker-0.0.1/docs/contributing/index.rst +28 -0
- opencontracting_nutcracker-0.0.1/docs/index.rst +105 -0
- opencontracting_nutcracker-0.0.1/docs/requirements.txt +1 -0
- opencontracting_nutcracker-0.0.1/nutcracker/__init__.py +380 -0
- opencontracting_nutcracker-0.0.1/nutcracker/__main__.py +37 -0
- opencontracting_nutcracker-0.0.1/nutcracker/archive.py +166 -0
- opencontracting_nutcracker-0.0.1/nutcracker/exceptions.py +9 -0
- opencontracting_nutcracker-0.0.1/nutcracker/pdf.py +207 -0
- opencontracting_nutcracker-0.0.1/nutcracker/util.py +57 -0
- opencontracting_nutcracker-0.0.1/nutcracker/word.py +365 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/PKG-INFO +41 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/SOURCES.txt +29 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/dependency_links.txt +1 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/entry_points.txt +2 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/requires.txt +10 -0
- opencontracting_nutcracker-0.0.1/opencontracting_nutcracker.egg-info/top_level.txt +3 -0
- opencontracting_nutcracker-0.0.1/pyproject.toml +70 -0
- opencontracting_nutcracker-0.0.1/setup.cfg +4 -0
- opencontracting_nutcracker-0.0.1/tests/__init__.py +68 -0
- opencontracting_nutcracker-0.0.1/tests/test_archive.py +122 -0
- opencontracting_nutcracker-0.0.1/tests/test_converter.py +289 -0
- opencontracting_nutcracker-0.0.1/tests/test_main.py +29 -0
- opencontracting_nutcracker-0.0.1/tests/test_word.py +167 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Open Contracting Partnership
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opencontracting-nutcracker
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Convert tender documents, including archives and scans, to text
|
|
5
|
+
Author-email: Open Contracting Partnership <data@open-contracting.org>
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/open-contracting/nutcracker
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Description-Content-Type: text/x-rst
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: click
|
|
19
|
+
Requires-Dist: openpyxl
|
|
20
|
+
Requires-Dist: xlrd
|
|
21
|
+
Provides-Extra: pdf
|
|
22
|
+
Requires-Dist: pymupdf; extra == "pdf"
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Requires-Dist: coverage; extra == "test"
|
|
25
|
+
Requires-Dist: pytest; extra == "test"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
|PyPI Version| |Build Status| |Coverage Status| |Python Version|
|
|
29
|
+
|
|
30
|
+
Nutcracker converts tender documents to text, page by page: PDFs (with OCR of scanned pages), Word, OpenDocument, RTF, spreadsheets, HTML, XML, text, images, and the members of ZIP, 7z and RAR archives, including split and nested archives.
|
|
31
|
+
|
|
32
|
+
(If you are viewing this on GitHub, open the `full documentation <https://ocp-nutcracker.readthedocs.io/>`__ for additional details.)
|
|
33
|
+
|
|
34
|
+
.. |PyPI Version| image:: https://img.shields.io/pypi/v/opencontracting-nutcracker.svg
|
|
35
|
+
:target: https://pypi.org/project/opencontracting-nutcracker/
|
|
36
|
+
.. |Build Status| image:: https://github.com/open-contracting/nutcracker/actions/workflows/ci.yml/badge.svg
|
|
37
|
+
:target: https://github.com/open-contracting/nutcracker/actions/workflows/ci.yml
|
|
38
|
+
.. |Coverage Status| image:: https://codecov.io/github/open-contracting/nutcracker/graph/badge.svg
|
|
39
|
+
:target: https://codecov.io/github/open-contracting/nutcracker
|
|
40
|
+
.. |Python Version| image:: https://img.shields.io/pypi/pyversions/opencontracting-nutcracker.svg
|
|
41
|
+
:target: https://pypi.org/project/opencontracting-nutcracker/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|PyPI Version| |Build Status| |Coverage Status| |Python Version|
|
|
2
|
+
|
|
3
|
+
Nutcracker converts tender documents to text, page by page: PDFs (with OCR of scanned pages), Word, OpenDocument, RTF, spreadsheets, HTML, XML, text, images, and the members of ZIP, 7z and RAR archives, including split and nested archives.
|
|
4
|
+
|
|
5
|
+
(If you are viewing this on GitHub, open the `full documentation <https://ocp-nutcracker.readthedocs.io/>`__ for additional details.)
|
|
6
|
+
|
|
7
|
+
.. |PyPI Version| image:: https://img.shields.io/pypi/v/opencontracting-nutcracker.svg
|
|
8
|
+
:target: https://pypi.org/project/opencontracting-nutcracker/
|
|
9
|
+
.. |Build Status| image:: https://github.com/open-contracting/nutcracker/actions/workflows/ci.yml/badge.svg
|
|
10
|
+
:target: https://github.com/open-contracting/nutcracker/actions/workflows/ci.yml
|
|
11
|
+
.. |Coverage Status| image:: https://codecov.io/github/open-contracting/nutcracker/graph/badge.svg
|
|
12
|
+
:target: https://codecov.io/github/open-contracting/nutcracker
|
|
13
|
+
.. |Python Version| image:: https://img.shields.io/pypi/pyversions/opencontracting-nutcracker.svg
|
|
14
|
+
:target: https://pypi.org/project/opencontracting-nutcracker/
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Minimal makefile for Sphinx documentation
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# You can set these variables from the command line, and also
|
|
5
|
+
# from the environment for the first two.
|
|
6
|
+
SPHINXOPTS ?=
|
|
7
|
+
SPHINXBUILD ?= sphinx-build
|
|
8
|
+
SOURCEDIR = .
|
|
9
|
+
BUILDDIR = _build
|
|
10
|
+
|
|
11
|
+
# Put it first so that "make" without argument is like "make help".
|
|
12
|
+
help:
|
|
13
|
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
14
|
+
|
|
15
|
+
.PHONY: help Makefile
|
|
16
|
+
|
|
17
|
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
|
18
|
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
|
19
|
+
%: Makefile
|
|
20
|
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Changelog
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
0.0.1 (2026-11-29)
|
|
5
|
+
------------------
|
|
6
|
+
|
|
7
|
+
First release, with the per-file conversion of ``evals/scripts/build_corpus.py`` in the oecd-poland repository. Its output is the same, except:
|
|
8
|
+
|
|
9
|
+
- A failure of pandoc or ``textutil`` is reported as ``ConversionError``, not ``RuntimeError``.
|
|
10
|
+
- An error message names the file instead of giving its path, and has no temporary paths, so that it doesn't vary between machines or runs.
|
|
11
|
+
- Without ``textutil``, .doc, .rtf, .odt and .ods files are converted with LibreOffice, else pandoc (.rtf and .odt).
|
|
12
|
+
- If 7-Zip's ``7zz`` command isn't installed, ``7z`` is used.
|
|
13
|
+
- If ``unar`` isn't installed, its absence is reported, instead of stopping the conversion.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Configuration file for the Sphinx documentation builder.
|
|
2
|
+
#
|
|
3
|
+
# For the full list of built-in configuration values, see the documentation:
|
|
4
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
10
|
+
|
|
11
|
+
# -- Project information -----------------------------------------------------
|
|
12
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
|
13
|
+
|
|
14
|
+
project = "Nutcracker"
|
|
15
|
+
copyright = "2026, Open Contracting Partnership"
|
|
16
|
+
author = "Open Contracting Partnership"
|
|
17
|
+
|
|
18
|
+
version = "0.0.1"
|
|
19
|
+
release = version
|
|
20
|
+
|
|
21
|
+
# -- General configuration ---------------------------------------------------
|
|
22
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
|
23
|
+
|
|
24
|
+
extensions = [
|
|
25
|
+
"sphinx.ext.autodoc",
|
|
26
|
+
"sphinx.ext.intersphinx",
|
|
27
|
+
"sphinx.ext.viewcode",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
templates_path = ["_templates"]
|
|
31
|
+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
|
|
32
|
+
|
|
33
|
+
# -- Options for HTML output -------------------------------------------------
|
|
34
|
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
|
35
|
+
|
|
36
|
+
html_theme = "furo"
|
|
37
|
+
html_static_path = []
|
|
38
|
+
|
|
39
|
+
# -- Extension configuration -------------------------------------------------
|
|
40
|
+
|
|
41
|
+
autodoc_default_options = {
|
|
42
|
+
"members": None,
|
|
43
|
+
"member-order": "bysource",
|
|
44
|
+
}
|
|
45
|
+
autodoc_typehints = "description"
|
|
46
|
+
autodoc_type_aliases = {}
|
|
47
|
+
|
|
48
|
+
intersphinx_mapping = {
|
|
49
|
+
"python": ("https://docs.python.org/3", None),
|
|
50
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Contributing
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
Setup
|
|
5
|
+
-----
|
|
6
|
+
|
|
7
|
+
Set up the git pre-commit hook:
|
|
8
|
+
|
|
9
|
+
.. code-block:: bash
|
|
10
|
+
|
|
11
|
+
pip install pre-commit
|
|
12
|
+
pre-commit install
|
|
13
|
+
|
|
14
|
+
Install the command-line tools listed in :doc:`../index`. Tests that need a missing tool are skipped.
|
|
15
|
+
|
|
16
|
+
Run the tests:
|
|
17
|
+
|
|
18
|
+
.. code-block:: bash
|
|
19
|
+
|
|
20
|
+
pip install -e .[pdf,test]
|
|
21
|
+
pytest -W error
|
|
22
|
+
|
|
23
|
+
Change the output
|
|
24
|
+
-----------------
|
|
25
|
+
|
|
26
|
+
A change to how files are converted changes the output of :meth:`~nutcracker.Converter.convert`. Release it as a new version, so that cached conversions are invalidated.
|
|
27
|
+
|
|
28
|
+
A change to how pages are OCR'd (like the resolution) must increment ``OCR_VERSION`` in ``nutcracker/pdf.py``, which invalidates the OCR store. Otherwise, the OCR store's folder name must stay the same for the same tools.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Nutcracker |release|
|
|
2
|
+
====================
|
|
3
|
+
|
|
4
|
+
.. include:: ../README.rst
|
|
5
|
+
|
|
6
|
+
Installation
|
|
7
|
+
------------
|
|
8
|
+
|
|
9
|
+
.. code-block:: bash
|
|
10
|
+
|
|
11
|
+
pip install opencontracting-nutcracker[pdf]
|
|
12
|
+
|
|
13
|
+
The ``pdf`` extra installs `PyMuPDF <https://pymupdf.readthedocs.io/>`__, which is needed to convert PDFs and images. PyMuPDF is licensed under the AGPL, so it is optional.
|
|
14
|
+
|
|
15
|
+
Nutcracker calls these command-line tools, if installed:
|
|
16
|
+
|
|
17
|
+
.. list-table::
|
|
18
|
+
:header-rows: 1
|
|
19
|
+
|
|
20
|
+
* - Tool
|
|
21
|
+
- Converts
|
|
22
|
+
* - `pandoc <https://pandoc.org>`__
|
|
23
|
+
- .docx, .docm, .pptx, .html, .htm (and .odt, .rtf, as below)
|
|
24
|
+
* - `Tesseract <https://tesseract-ocr.github.io>`__, with the Polish (``pol``) and orientation (``osd``) models
|
|
25
|
+
- PDF pages without a text layer, and images
|
|
26
|
+
* - `7-Zip <https://www.7-zip.org>`__ (``7zz`` or ``7z``)
|
|
27
|
+
- .7z, .rar, split archives, and ZIPs that Python can't read
|
|
28
|
+
* - `The Unarchiver <https://theunarchiver.com/command-line>`__ (``unar``)
|
|
29
|
+
- RAR archives that 7-Zip can't decompress
|
|
30
|
+
* - ``textutil`` (macOS)
|
|
31
|
+
- .doc, .rtf, .odt, .ods
|
|
32
|
+
* - `LibreOffice <https://www.libreoffice.org>`__ (``soffice``)
|
|
33
|
+
- .doc, .rtf, .odt, .ods, without ``textutil``
|
|
34
|
+
|
|
35
|
+
Without ``textutil`` or LibreOffice, .rtf and .odt files are converted with pandoc, and .doc and .ods files fail to convert. The output of ``textutil`` and of LibreOffice differ, so the same .doc file can yield different text on macOS and Linux.
|
|
36
|
+
|
|
37
|
+
Usage
|
|
38
|
+
-----
|
|
39
|
+
|
|
40
|
+
From the command line, print a file's text:
|
|
41
|
+
|
|
42
|
+
.. code-block:: bash
|
|
43
|
+
|
|
44
|
+
nutcracker path/to/file.pdf
|
|
45
|
+
|
|
46
|
+
Or an archive's text, with a ``# member`` line before each member:
|
|
47
|
+
|
|
48
|
+
.. code-block:: bash
|
|
49
|
+
|
|
50
|
+
nutcracker path/to/archive.zip
|
|
51
|
+
|
|
52
|
+
Run ``nutcracker --help`` for options.
|
|
53
|
+
|
|
54
|
+
From Python:
|
|
55
|
+
|
|
56
|
+
.. code-block:: python
|
|
57
|
+
|
|
58
|
+
from nutcracker import Converter, render
|
|
59
|
+
|
|
60
|
+
converter = Converter(cache_dir="cache", ocr_dir="cache/ocr")
|
|
61
|
+
data = converter.convert("archive.zip")
|
|
62
|
+
for item in data["items"]:
|
|
63
|
+
print(render(item["pages"], item["flags"], header=f"# {item['member']}"))
|
|
64
|
+
for error in data["errors"]:
|
|
65
|
+
print(error)
|
|
66
|
+
|
|
67
|
+
A file that fails to convert is left out of ``items``, and its error is reported in ``errors``. Members of archives in formats that aren't converted (like CAD drawings) are reported in ``skipped``.
|
|
68
|
+
|
|
69
|
+
How files are converted
|
|
70
|
+
~~~~~~~~~~~~~~~~~~~~~~~
|
|
71
|
+
|
|
72
|
+
PDF
|
|
73
|
+
Each page's text layer is extracted. A page up to A3 without a text layer, or with little text under an image that covers it (a scan under a stamp), is OCR'd at 300 dpi; if Tesseract's orientation detection suggests a rotation, the more confident reading is kept. Struck-through words are wrapped in ``[skreślono: …]``. A larger page without text is flagged as a ``drawing``, and a page without text as ``no text``.
|
|
74
|
+
Images
|
|
75
|
+
OCR'd like PDF pages.
|
|
76
|
+
Word (.docx, .docm)
|
|
77
|
+
Converted by pandoc, after list numbers are made literal (as Word displays them) and struck-through text is wrapped in ``[skreślono: …]``.
|
|
78
|
+
Word (.doc), RTF, OpenDocument text (.odt)
|
|
79
|
+
Converted by ``textutil``, unless it has struck-through text, in which case it is converted to .docx first.
|
|
80
|
+
Spreadsheets (.xlsx, .xlsm, .xls, .ods)
|
|
81
|
+
One page per sheet, with tab-separated cells. Runs of identical rows are collapsed, and sheets are truncated after 5,000 rows.
|
|
82
|
+
XML
|
|
83
|
+
The text content, joined by spaces.
|
|
84
|
+
Text
|
|
85
|
+
Decoded as UTF-8, else Windows-1250, else ISO-8859-1.
|
|
86
|
+
|
|
87
|
+
A file whose content doesn't match its extension (like a PDF named .doc) is converted according to its content.
|
|
88
|
+
|
|
89
|
+
Caching
|
|
90
|
+
~~~~~~~
|
|
91
|
+
|
|
92
|
+
With ``cache_dir``, a conversion is cached by the files' names and contents (SHA-256), whether OCR is on, and the versions of Nutcracker and of the tools and libraries that shape its output. A new version of any of these is a cache miss.
|
|
93
|
+
|
|
94
|
+
With ``ocr_dir``, OCR text is stored by page, by the file's content and by the versions of Tesseract, its models and MuPDF. A file keeps its OCR text if it is renamed or repacked in another archive.
|
|
95
|
+
|
|
96
|
+
To OCR many files in parallel, set ``ocr_queue_dir`` and convert the files: pages missing from the OCR store are queued in :attr:`~nutcracker.Converter.ocr_queue` instead of OCR'd. Then, OCR them in parallel with :meth:`~nutcracker.Converter.ocr_pages` and :meth:`~nutcracker.Converter.save_ocr`, and convert the files again without ``ocr_queue_dir``.
|
|
97
|
+
|
|
98
|
+
.. toctree::
|
|
99
|
+
:caption: Contents
|
|
100
|
+
|
|
101
|
+
api/index
|
|
102
|
+
contributing/index
|
|
103
|
+
changelog
|
|
104
|
+
|
|
105
|
+
Copyright (c) 2026 Open Contracting Partnership, released under the BSD license
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
furo
|