Skip to main content

Unified File Reader

A clean, extensible Python library for reading multiple file formats with a unified API. Built following Clean Code and Clean Architecture principles.

Features

  • Unified API: Single read_file() function for all supported formats
  • Multiple Formats: CSV, JSON, TXT, XLSX, PDF, DOCX
  • Clean Architecture: Modular, testable, and maintainable codebase
  • Extensible: Add new file readers without modifying core logic
  • Type-Safe: Full type hints for better IDE support
  • Well-Tested: Comprehensive test suite with high coverage
  • Production-Ready: PyPI packaging, logging, and error handling

Requirements

Developed and tested on Python 3.12.8 (recommended). Other versions within 3.9–3.12 should also work.

  • pip (latest version recommended)
  • Virtual environment tool (python -m venv)

See requirements.txt for the exact dependency versions used with Python 3.12.8.

Installation

From PyPI

pip install unified-file-reader

From source (development setup)

# Clone the repository
git clone https://github.com/praveen9392/unified-file-reader.git
cd unified-file-reader

# Create a virtual environment (recommended)
python -m venv .venv

# Activate
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# Windows (cmd)
.venv\Scripts\activate.bat
# macOS / Linux
source .venv/bin/activate

# Install runtime + dev dependencies
pip install -e ".[dev]"

# Alternatively, to match exact versions from this repo
pip install -r requirements.txt

Quick Start

from unified_file_reader import read_file

# Read any supported file format
data = read_file("data.csv")
data = read_file("config.json")
data = read_file("document.pdf")
data = read_file("spreadsheet.xlsx")

Supported Formats

Format Extension Return Type Notes
CSV .csv List[Dict] Returns list of dictionaries
JSON .json Any Preserves JSON structure
TXT .txt str Returns plain text content
Excel .xlsx List[Dict] Returns list of dictionaries
PDF .pdf str Extracts all text content
DOCX .docx str Extracts all paragraph text

Architecture Overview

unified_file_reader/
├── api.py                 # Public API entry point
├── registry.py            # Reader registry (dependency inversion)
├── exceptions.py          # Custom exceptions
├── interfaces/
│   └── base_reader.py     # Abstract reader contract
├── readers/               # Concrete implementations
│   ├── csv_reader.py
│   ├── json_reader.py
│   ├── txt_reader.py
│   ├── excel_reader.py
│   ├── pdf_reader.py
│   └── docx_reader.py
├── utils/
│   └── file_utils.py      # Utility functions
└── config/
    └── supported_formats.py

Design Principles

  • Single Responsibility Principle (SRP): Each reader handles one format
  • Open/Closed Principle (OCP): Add new readers without modifying existing code
  • Liskov Substitution Principle (LSP): All readers implement the same interface
  • Interface Segregation Principle (ISP): Minimal, focused interfaces
  • Dependency Inversion Principle (DIP): High-level modules depend on abstractions

Usage Examples

Reading CSV Files

from unified_file_reader import read_file

data = read_file("employees.csv")
# Returns: [{"name": "John", "age": "30"}, {"name": "Jane", "age": "28"}]

for row in data:
    print(f"{row['name']}: {row['age']}")

Reading JSON Files

config = read_file("config.json")
# Returns: {"database": {"host": "localhost", "port": 5432}}

print(config["database"]["host"])

Reading Text Files

content = read_file("document.txt")
# Returns: "This is the file content..."

print(content)

Reading Excel Files

data = read_file("report.xlsx")
# Returns: [{"Quarter": "Q1", "Revenue": 100000}, ...]

for row in data:
    print(f"{row['Quarter']}: ${row['Revenue']}")

Reading PDF Files

text = read_file("document.pdf")
# Returns: "Page 1 content...\nPage 2 content..."

print(text)

Reading DOCX Files

text = read_file("report.docx")
# Returns: "Paragraph 1...\nParagraph 2..."

print(text)

Error Handling

from unified_file_reader import read_file
from unified_file_reader.exceptions import UnsupportedFormatError, ReaderError

try:
    data = read_file("file.xyz")
except UnsupportedFormatError as e:
    print(f"Format not supported: {e}")
except FileNotFoundError as e:
    print(f"File not found: {e}")
except ReaderError as e:
    print(f"Reading error: {e}")

Extending with New Readers

To add support for a new file format:

  1. Create a new reader class in unified_file_reader/readers/:
from unified_file_reader.interfaces.base_reader import BaseReader
from unified_file_reader.utils.file_utils import ensure_file_exists

class YAMLReader(BaseReader):
    supported_extensions = [".yaml", ".yml"]

    def can_read(self, extension: str) -> bool:
        return extension in self.supported_extensions

    def read(self, path: str):
        ensure_file_exists(path)
        import yaml
        with open(path, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)
  1. Register it in unified_file_reader/registry.py:
from unified_file_reader.readers.yaml_reader import YAMLReader

readers = [
    # ... existing readers ...
    YAMLReader(),
]
  1. Add tests in tests/test_yaml_reader.py

  2. Update supported_formats.py with metadata

That's it! No changes needed to the core API.

Testing

Run the test suite:

pytest

With coverage:

pytest --cov=unified_file_reader

Run specific tests:

pytest tests/test_api.py -v

Development

Install development dependencies:

pip install -e ".[dev]"

Format code:

black unified_file_reader tests
isort unified_file_reader tests

Type checking:

mypy unified_file_reader

Linting:

flake8 unified_file_reader tests

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details

Changelog

v0.1.2

  • Modernized dev dependencies (reportlab for complete PDF testing)
  • Aligned static type hints with strict mypy compliance
  • Harmonized Flake8 configuration (max-line-length = 100) with Black and isort
  • Added GitHub Pull Request template and CHANGELOG.md
  • 100% test pass rate (93 passed, 0 skipped, 0 failed)

v0.1.1

  • Updated PyPI package version and metadata
  • Enhanced Python 3.12 compatibility

v0.1.0 (Initial Release)

  • Initial release with support for CSV, JSON, TXT, XLSX, PDF, DOCX
  • Clean Architecture implementation
  • Full test coverage
  • PyPI packaging

Support

For issues, questions, or suggestions, please open an issue on GitHub.

Metadata

Release files for unified-file-reader 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unified-file-reader 0.1.2
File Size Uploaded Provenance Actions
unified_file_reader-0.1.2.tar.gz 20.8 kB Without provenance Details for unified_file_reader-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unified-file-reader 0.1.2
File Interpreter ABI Platform Actions
unified_file_reader-0.1.2-py3-none-any.whl Python 3 none any Details for unified_file_reader-0.1.2-py3-none-any.whl

Total release size: 37.1 kB

Release files / unified_file_reader-0.1.2.tar.gz

Download URL unified_file_reader-0.1.2.tar.gz
Size 20.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0c09def987acda79e29e9969673f41703e91c51b1850630fbe55b9424e46edc7
BLAKE2b-256 checksum
How to use checksums
f1e0beecca43dc64f4fc9f66f690d6d0bd118d4c74f3f82774a6c30342cbb963
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release files / unified_file_reader-0.1.2-py3-none-any.whl

Download URL unified_file_reader-0.1.2-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8354cd44308073f8cde1eaba4f6871e5a1c2133227e2826469e4e09dd8616c75
BLAKE2b-256 checksum
How to use checksums
dd1e379fea4af0027c29e39b88ae29262fe5093c5b2a741ba572c7a63a128ee5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Security for unified-file-reader 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page