Python library for post-extraction refinement of text that may be derived from PDF extraction.
Project description
Refinedoc
Python library for extracting headers, footers and body from PDF post parsing file by the Learning Planet Institute.
Table of Contents
Why using Refinedoc ?
The idea behind this library is to enable post-extraction processing of unstructured text content, the best-known example being pdf files. The main idea is to robustly and securely separate the text body from its headers and footers.
What's more, the lib is written in pure Python and has no dependencies other than the standard lib.
Features
- Header and Footer Extraction: Automatically identifies and extracts headers and footers from the document.
- Body Extraction: Separates the main content of the document from headers and footers.
- Page Association: Uses page association techniques to ensure accurate extraction of headers and footers across multiple pages.
- Robustness: Designed to handle various document structures and formats, ensuring reliable extraction even in complex layouts.
- Pure Python Implementation: No external dependencies, making it easy to integrate into existing Python projects.
- Easy to Use: Simple API for extracting headers, footers, and body content from documents.
- Compatibility: Works with text extracted from PDF files using libraries like PyPDF, PyMuPDF, and pdfplumber.
- Performance: Efficiently processes large documents with minimal overhead.
- Open Source: Licensed under Apache 2.0, allowing for free use and modification in both personal and commercial projects.
Quickstart
Requirements
- Python 3.10 <=
Installation
You can install with pip
pip install refinedoc
Example (vanilla)
from refinedoc.refined_document import RefinedDocument
document = [
[
"header 1",
"subheader 1",
"lorem ipsum dolor sit amet",
"consectetur adipiscing elit",
"footer 1",
],
[
"header 2",
"subheader 2",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
"footer 2",
],
[
"header 3",
"subheader 3",
"ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
"footer 3",
],
[
"header 4",
"subheader 4",
"duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur",
"footer 4",
],
]
rd = RefinedDocument(content=document)
headers = rd.headers
# [["header 1", "subheader 1"], ["header 2", "subheader 2"], ["header 3", "subheader 3"], ["header 4", "subheader 4"]]
footers = rd.footers
# [["footer 1"], ["footer 2"], ["footer 3"], ["footer 4"]]
body = rd.body
# [["lorem ipsum dolor sit amet", "consectetur adipiscing elit"], ["sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"], ["ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"], ["duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"]]
Example (with pypdf)
from refinedoc.refined_document import RefinedDocument
from pypdf import PdfReader
# Build the document from a PDF file
reader = PdfReader("path/to/your/pdf/file.pdf")
document = []
for page in reader.pages:
document.append(page.extract_text().split("\n"))
rd = RefinedDocument(content=document)
headers = rd.headers
# [["header 1", "subheader 1"], ["header 2", "subheader 2"], ["header 3", "subheader 3"], ["header 4", "subheader 4"]]
footers = rd.footers
# [["footer 1"], ["footer 2"], ["footer 3"], ["footer 4"]]
body = rd.body
# [["lorem ipsum dolor sit amet", "consectetur adipiscing elit"], ["sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"], ["ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"], ["duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"]]
Example (with pymupdf)
from refinedoc.refined_document import RefinedDocument
import pymupdf # PyMuPDF
# Build the document from a PDF file
doc = fitz.open("path/to/your/pdf/file.pdf")
document = []
for page in doc:
text = page.get_text("text").split("\n")
document.append(text)
rd = RefinedDocument(content=document)
headers = rd.headers
# [["header 1", "subheader 1"], ["header 2", "subheader 2"], ["header 3", "subheader 3"], ["header 4", "subheader 4"]]
footers = rd.footers
# [["footer 1"], ["footer 2"], ["footer 3"], ["footer 4"]]
body = rd.body
# [["lorem ipsum dolor sit amet", "consectetur adipiscing elit"], ["sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"], ["ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"], ["duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"]]
Example (with pdfplumber)
from refinedoc.refined_document import RefinedDocument
import pdfplumber
# Build the document from a PDF file
with pdfplumber.open("path/to/your/pdf/file.pdf") as pdf:
document = []
for page in pdf.pages:
text = page.extract_text().split("\n")
document.append(text)
rd = RefinedDocument(content=document)
headers = rd.headers
# [["header 1", "subheader 1"], ["header 2", "subheader 2"], ["header 3", "subheader 3"], ["header 4", "subheader 4"]]
footers = rd.footers
# [["footer 1"], ["footer 2"], ["footer 3"], ["footer 4"]]
body = rd.body
# [["lorem ipsum dolor sit amet", "consectetur adipiscing elit"], ["sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"], ["ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"], ["duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"]]
Advanced usage
Improving speed
You can improve the speed of extraction by using ratio_speed parameter when instantiating class.
Ratio speed is an integer between 1 and 3. More the ratio is high, more the extraction will be fast but less accurate.
These ratio are proxy for parameter from SequenceMatcher from difflib library.
ratio_speed=1is equivalent to using regular ratioratio_speed=2is equivalent to using quick_ratioratio_speed=3is equivalent to using real_quick_ratio
RefinedDocument(content=document, ratio_speed=2)
Customize windows size
The win parameter in the RefinedDocument class defines the size of the window used to detect similar headers and footers between pages in the document. When separating headers/footers, it determines how many neighbouring pages (before and after the current page) are taken into account to compare and identify repetitive lines. A larger value increases the scope of the comparison, which can improve detection but slow down processing.
from refinedoc.refined_document import RefinedDocument
# 10 pages before and after the current page will be considered for
# header/footer detection
RefinedDocument(content=document, win=10)
How it's work
My work is based on this paper : Lin, Xiaofan. (2003). Header and Footer Extraction by Page-Association. 5010. 164-171. 10.1117/12.472833.
And an article medium by Hussain Shahbaz Khawaja.
License
This projects is licensed under Apache 2.0 License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file refinedoc-1.0.1.tar.gz.
File metadata
- Download URL: refinedoc-1.0.1.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afad515bcbd13e9e7ba40d149d6b2a8a04c88323bc31eef2eddf3e3bbd6e921b
|
|
| MD5 |
5c9b35a4ccd37a51d13d6c31181e8e8c
|
|
| BLAKE2b-256 |
ef048620d4ac90ea5242d90dd6dd5736776818a3e3225deac9872f703a1a39d9
|
Provenance
The following attestation bundles were made for refinedoc-1.0.1.tar.gz:
Publisher:
publish-to-test-pypi.yml on CyberCRI/refinedoc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
refinedoc-1.0.1.tar.gz -
Subject digest:
afad515bcbd13e9e7ba40d149d6b2a8a04c88323bc31eef2eddf3e3bbd6e921b - Sigstore transparency entry: 444306016
- Sigstore integration time:
-
Permalink:
CyberCRI/refinedoc@369a059972ee5e5bcc42edd4e3daee442e8eec22 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/CyberCRI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@369a059972ee5e5bcc42edd4e3daee442e8eec22 -
Trigger Event:
push
-
Statement type:
File details
Details for the file refinedoc-1.0.1-py3-none-any.whl.
File metadata
- Download URL: refinedoc-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2840fefb110b33d84474ef3b5e3b3813a55fb7c8e42a70615f51acb1b3885c7
|
|
| MD5 |
c66e10660776826c3f3467055263cee1
|
|
| BLAKE2b-256 |
2cb252a0b8c42a4a0574ec77a45ea693bf6e030357deed49f2011750a51ffff1
|
Provenance
The following attestation bundles were made for refinedoc-1.0.1-py3-none-any.whl:
Publisher:
publish-to-test-pypi.yml on CyberCRI/refinedoc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
refinedoc-1.0.1-py3-none-any.whl -
Subject digest:
a2840fefb110b33d84474ef3b5e3b3813a55fb7c8e42a70615f51acb1b3885c7 - Sigstore transparency entry: 444306031
- Sigstore integration time:
-
Permalink:
CyberCRI/refinedoc@369a059972ee5e5bcc42edd4e3daee442e8eec22 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/CyberCRI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@369a059972ee5e5bcc42edd4e3daee442e8eec22 -
Trigger Event:
push
-
Statement type: