Skip to content

Commit

Permalink
Merge pull request #4 from renan-siqueira/develop
Browse files Browse the repository at this point in the history
Merge develop into main
  • Loading branch information
renan-siqueira authored Nov 17, 2023
2 parents 749f68c + cdcc4a3 commit 6e9b0a6
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 renan-siqueira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# PDF Text Extraction Tool

## Description

This project facilitates the extraction of text from PDF files using various Python libraries. It is designed to be flexible, allowing the choice among different text extraction libraries and supporting both single PDF file and directory containing multiple PDF files.

---

## Project Structure

```markdown
- main.py
- extractors/
- __init__.py
- pypdf2_extractor.py
- pdfminer_extractor.py
- pymupdf_extractor.py
- pdfplumber_extractor.py
- helpers/
- __init__.py
- utils.py
- json/
- params.json
```

---

## Configuration

- **Python:** This project is developed in Python. Ensure you have the latest version of Python installed.
- **Dependencies:** The required libraries are listed in each extraction file within the `extractors` folder. Install them using `pip install <library>`.

_Use the `requirements.txt` file to install all libraries at once_

---

## Usage

1. **Initial Setup:** Edit the `json/params.json` file to set the input path (`input_path`), output path (`output_path`), desired libraries (`libraries`), and log level (`log_level`).

Example `params.json`:

```json
{
"input_path": "/path/to/pdf/or/directory",
"output_path": "/path/to/output/directory",
"libraries": ["pypdf2", "pdfminer"],
"log_level": "INFO"
}
```

2. **Execution:** Run the `main.py` script to start the text extraction process.

```bash
python main.py
```

---

## Features

- Text extraction from PDF files using various libraries.
- Supports processing either a single file or multiple files in a directory.
- Automatic output folder generation based on input.
- Flexible configuration via the json/params.json file.
Empty file added extractors/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions extractors/pdfminer_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pdfminer.high_level import extract_text


def extract_text(file_path):
return extract_text(file_path)
8 changes: 8 additions & 0 deletions extractors/pdfplumber_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pdfplumber


def extract_text(file_path):
text = ""
with pdfplumber.open(file_path) as pdf:
text = ''.join(page.extract_text() or '' for page in pdf.pages)
return text
8 changes: 8 additions & 0 deletions extractors/pymupdf_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fitz as PyMuPDF


def extract_text(file_path):
text = ""
with PyMuPDF.open(file_path) as doc:
text = ''.join(page.get_text() for page in doc)
return text
9 changes: 9 additions & 0 deletions extractors/pypdf2_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import PyPDF2


def extract_text(file_path):
text = ""
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''.join(page.extract_text() or '' for page in reader.pages)
return text
Empty file added helpers/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions helpers/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def save_text_to_file(text, file_path):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(text)
6 changes: 6 additions & 0 deletions json/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input_path": "/path/to/pdf/or/directory",
"output_path": "/path/to/output/directory",
"libraries": ["pypdf2", "pdfminer"],
"log_level": "INFO"
}
62 changes: 62 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import os
import logging
from extractors import pypdf2_extractor, pdfminer_extractor, pymupdf_extractor, pdfplumber_extractor
from helpers import utils


def get_extractor(library_name):
return {
'pypdf2': pypdf2_extractor.extract_text,
'pdfminer': pdfminer_extractor.extract_text,
'pymupdf': pymupdf_extractor.extract_text,
'pdfplumber': pdfplumber_extractor.extract_text
}.get(library_name, None)


def process_file(file_path, libraries, output_dir):
for library in libraries:
extractor = get_extractor(library)
if extractor:
try:
extracted_text = extractor(file_path)
output_file = os.path.join(output_dir, f"{os.path.basename(file_path).split('.')[0]}_{library}.txt")
utils.save_text_to_file(extracted_text, output_file)
except Exception as e:
logging.error(f"Error processing {file_path} with {library}: {e}")


def create_output_dir(base_dir, input_path, is_single_file):
if is_single_file:
output_dir = os.path.join(base_dir, os.path.basename(input_path).split('.')[0])
else:
output_dir = os.path.join(base_dir, os.path.basename(input_path))
os.makedirs(output_dir, exist_ok=True)
return output_dir


def main():
with open('json/config.json') as config_file:
config = json.load(config_file)

logging.basicConfig(level=config.get("log_level", "INFO"))

input_path = config["input_path"]
libraries = config["libraries"]
base_output_dir = config.get("output_path", "./output")

is_single_file = os.path.isfile(input_path) and input_path.endswith('.pdf')
output_dir = create_output_dir(base_output_dir, input_path, is_single_file)

if is_single_file:
process_file(input_path, libraries, output_dir)
elif os.path.isdir(input_path):
for filename in os.listdir(input_path):
if filename.endswith('.pdf'):
process_file(os.path.join(input_path, filename), libraries, output_dir)
else:
logging.error("Invalid input path")


if __name__ == '__main__':
main()
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cffi==1.16.0
charset-normalizer==3.3.2
cryptography==41.0.5
pdfminer==20191125
pdfminer.six==20221105
pdfplumber==0.10.3
Pillow==10.1.0
pycparser==2.21
pycryptodome==3.19.0
PyMuPDF==1.23.6
PyMuPDFb==1.23.6
PyPDF2==3.0.1
pypdfium2==4.24.0

0 comments on commit 6e9b0a6

Please sign in to comment.