Skip to content

laguileracl/leychile-epub

📚 LeyChile ePub Generator

CI PyPI version Python 3.10+ License: MIT Code style: black Ruff Contributions Welcome

🇨🇱 Generador de libros electrónicos (ePub) a partir de la legislación chilena oficial de la Biblioteca del Congreso Nacional.

Convierte cualquier ley, decreto o norma chilena en un libro electrónico profesional listo para leer en tu Kindle, Kobo, iPad o cualquier e-reader.

English | Español


📖 Tabla de Contenidos


✨ Características

🎯 Funcionalidades Principales

  • Scraping Inteligente: Extrae datos directamente de la API XML oficial de la BCN
  • ePub Profesional: Genera libros electrónicos con formato premium
  • Múltiples Interfaces: CLI, Web (Streamlit) y API Python
  • Procesamiento por Lotes: Convierte múltiples normas de una vez

📱 Características del ePub Generado

Característica Descripción
📑 Portada Personalizada Incluye título, tipo de norma y fecha de publicación
📚 Tabla de Contenidos Navegación interactiva por títulos, capítulos y artículos
🎨 Estilos Profesionales CSS optimizado para lectura cómoda
🔗 Referencias Cruzadas Links internos entre artículos relacionados
📇 Índice de Palabras Clave Búsqueda rápida de términos importantes
📋 Metadatos Completos Autor, fecha, identificadores estándar
Compatibilidad Universal Funciona en Kindle, Kobo, Apple Books, etc.

📜 Tipos de Normas Soportadas

  • ✅ Leyes
  • ✅ Decretos Ley (DL)
  • ✅ Decretos con Fuerza de Ley (DFL)
  • ✅ Decretos Supremos
  • ✅ Códigos (Civil, Penal, del Trabajo, etc.)
  • ✅ Constitución Política
  • ✅ Reglamentos
  • ✅ Y más...

🚀 Inicio Rápido

# Clonar el repositorio
git clone https://github.com/laguileracl/leychile-epub.git
cd leychile-epub

# Crear entorno virtual
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# Instalar el paquete
pip install -e .

# Generar tu primer ePub
leychile-epub https://www.leychile.cl/Navegar?idNorma=242302

¡Listo! Encontrarás el archivo ePub generado en tu directorio.


📦 Instalación

Requisitos

  • Python 3.10 o superior
  • pip (gestor de paquetes de Python)

Instalación Paso a Paso

  1. Clona el repositorio

    git clone https://github.com/laguileracl/leychile-epub.git
    cd leychile-epub
  2. Crea un entorno virtual (recomendado)

    python -m venv .venv
    
    # En macOS/Linux:
    source .venv/bin/activate
    
    # En Windows:
    .venv\Scripts\activate
  3. Instala el paquete

    # Instalación básica
    pip install -e .
    
    # Con dependencias de desarrollo
    pip install -e ".[dev]"
    
    # Con interfaz web (Streamlit)
    pip install -e ".[web]"

Dependencias

Paquete Versión Descripción
requests ≥2.28 Cliente HTTP para la API de BCN
beautifulsoup4 ≥4.11 Parser HTML/XML
lxml ≥4.9 Parser XML de alto rendimiento
ebooklib ≥0.18 Generación de archivos ePub
streamlit ≥1.28 Interfaz web (opcional)

💻 Uso

Línea de Comandos (CLI)

La forma más directa de usar el generador:

# Generar ePub de una ley específica
leychile-epub https://www.leychile.cl/Navegar?idNorma=61438

# También funciona con python -m
python -m leychile_epub https://www.leychile.cl/Navegar?idNorma=61438

# Especificar directorio de salida
leychile-epub https://www.leychile.cl/Navegar?idNorma=61438 -o ./mis_leyes/

# Modo silencioso (sin output en consola)
leychile-epub https://www.leychile.cl/Navegar?idNorma=61438 -q

# Modo verbose (más información)
leychile-epub https://www.leychile.cl/Navegar?idNorma=61438 -v

# Procesar múltiples URLs desde un archivo
leychile-epub --batch urls.txt -o ./output/

# Ver versión
leychile-epub --version

Opciones del CLI

Opción Corta Descripción
--output -o Directorio de salida para los ePub
--batch -b Archivo con lista de URLs (una por línea)
--quiet -q Modo silencioso
--verbose -v Modo verbose
--version Mostrar versión
--help -h Mostrar ayuda

Interfaz Web

Para una experiencia más visual:

python main.py --web
# o directamente:
streamlit run app.py

Esto abrirá una interfaz web en http://localhost:8501 con:

  • Campo para ingresar URLs
  • Vista previa del contenido
  • Opciones de personalización
  • Descarga directa del ePub

Nota: La interfaz web requiere Streamlit, que puede no ser compatible con Python 3.14+

Como Biblioteca Python

Integra el generador en tus propios proyectos:

from leychile_epub import BCNLawScraper, LawEpubGenerator

# Inicializar scraper
scraper = BCNLawScraper()

# Obtener datos de una ley
url = "https://www.leychile.cl/Navegar?idNorma=61438"
law_data = scraper.scrape_law(url)

if law_data:
    # Generar ePub
    generator = LawEpubGenerator()
    epub_path = generator.generate(law_data, output_dir="./output")
    print(f"ePub generado: {epub_path}")

API del Scraper

from leychile_epub import BCNLawScraper

scraper = BCNLawScraper()

# Obtener datos de una ley
law_data = scraper.scrape_law(url)

# Estructura de law_data:
{
    "id_norma": "61438",
    "url": "https://...",
    "id_version": "2024-01-15",
    "metadata": {
        "title": "Ley 18700",
        "type": "Ley",
        "number": "18700",
        "organism": "Ministerio del Interior",
        "source": "Diario Oficial",
        "subjects": ["Elecciones", "Votación"],
        "derogation_dates": [...],
    },
    "content": [
        {"type": "titulo", "text": "TITULO I..."},
        {"type": "articulo", "title": "Artículo 1", "text": "..."},
        ...
    ]
}

API del Generador

from leychile_epub import LawEpubGenerator, Config

# Configuración personalizada
config = Config.create_default()
config.epub.output_dir = "./mis_epubs"
config.epub.creator = "Mi Aplicación"

# Generar ePub
generator = LawEpubGenerator(config)
epub_path = generator.generate(
    law_data,
    output_dir="./output",      # Directorio de salida
    filename="mi_ley.epub",     # Nombre del archivo
    progress_callback=lambda p, msg: print(f"{p*100:.0f}%: {msg}")
)

📋 Ejemplos

Leyes Populares

# Código del Trabajo
leychile-epub https://www.leychile.cl/Navegar?idNorma=242302

# Código Civil
leychile-epub https://www.leychile.cl/Navegar?idNorma=172986

# Código Penal
leychile-epub https://www.leychile.cl/Navegar?idNorma=1984

# Constitución Política
leychile-epub https://www.leychile.cl/Navegar?idNorma=242302

# Ley de Tránsito
leychile-epub https://www.leychile.cl/Navegar?idNorma=29708

# Código de Aguas
leychile-epub https://www.leychile.cl/Navegar?idNorma=5605

Procesamiento por Lotes

Crea un archivo leyes.txt:

https://www.leychile.cl/Navegar?idNorma=242302
https://www.leychile.cl/Navegar?idNorma=172986
https://www.leychile.cl/Navegar?idNorma=1984

Ejecuta:

leychile-epub --batch leyes.txt -o ./biblioteca_legal/

Uso Programático Avanzado

from leychile_epub import BCNLawScraper, LawEpubGenerator
from pathlib import Path

def crear_biblioteca_legal(urls: list[str], output_dir: str = "./biblioteca"):
    """Crea una biblioteca de ePubs a partir de múltiples URLs."""
    scraper = BCNLawScraper()
    generator = LawEpubGenerator()
    output_path = Path(output_dir)
    output_path.mkdir(exist_ok=True)
    
    resultados = []
    for url in urls:
        try:
            law_data = scraper.scrape_law(url)
            if law_data:
                epub_path = generator.generate(law_data, output_dir=str(output_path))
                resultados.append({"url": url, "epub": epub_path, "status": "success"})
            else:
                resultados.append({"url": url, "status": "no_data"})
        except Exception as e:
            resultados.append({"url": url, "status": "error", "error": str(e)})
    
    return resultados

# Uso
urls = [
    "https://www.leychile.cl/Navegar?idNorma=242302",
    "https://www.leychile.cl/Navegar?idNorma=172986",
]
resultados = crear_biblioteca_legal(urls)

🏗️ Arquitectura

leychile-epub/
│
├── � src/leychile_epub/      # Paquete principal
│   ├── __init__.py            # Exports públicos
│   ├── __main__.py            # Entry point para python -m
│   ├── cli.py                 # Interfaz de línea de comandos
│   ├── config.py              # Configuración centralizada
│   ├── exceptions.py          # Excepciones personalizadas
│   ├── generator.py           # Generador de ePub
│   ├── scraper.py             # Scraper para la API de BCN
│   ├── styles.py              # Estilos CSS premium
│   └── py.typed               # Soporte para type checking
│
├── 📁 tests/                   # Tests unitarios
│   ├── test_config.py         # Tests de configuración
│   ├── test_scraper.py        # Tests del scraper
│   └── test_generator.py      # Tests del generador
│
├── 📁 docs/                    # Documentación adicional
│
├── 📄 pyproject.toml          # Configuración del proyecto
├── 📄 README.md               # Este archivo
├── 📄 CONTRIBUTING.md         # Guía de contribución
├── 📄 CODE_OF_CONDUCT.md      # Código de conducta
├── 📄 CHANGELOG.md            # Historial de cambios
├── 📄 LICENSE                 # Licencia MIT
└── 📄 SECURITY.md             # Política de seguridad

Flujo de Datos

┌─────────────┐     ┌──────────────┐     ┌────────────────┐     ┌──────────┐
│  URL BCN    │────▶│  BCNScraper  │────▶│ EpubGenerator  │────▶│  .epub   │
│  (input)    │     │  (extract)   │     │  (transform)   │     │ (output) │
└─────────────┘     └──────────────┘     └────────────────┘     └──────────┘
                           │                      │
                           ▼                      ▼
                    ┌──────────────┐     ┌────────────────┐
                    │  XML API     │     │  CSS Styles    │
                    │  BCN Chile   │     │  Templates     │
                    └──────────────┘     └────────────────┘

🤝 Contribuir

¡Las contribuciones son bienvenidas! Este es un proyecto open source creado para la comunidad.

Formas de Contribuir

  • 🐛 Reportar bugs: Abre un Issue
  • 💡 Sugerir mejoras: Comparte tus ideas
  • 📝 Mejorar documentación: Ayuda a otros usuarios
  • 🔧 Enviar código: Haz un Pull Request

Lee la Guía de Contribución para más detalles.

Desarrollo

# Clonar tu fork
git clone https://github.com/TU_USUARIO/leychile-epub.git
cd leychile-epub

# Crear branch para tu feature
git checkout -b feature/mi-mejora

# Hacer cambios y commit
git add .
git commit -m "feat: agregar mi mejora"

# Push y crear PR
git push origin feature/mi-mejora

📄 Licencia

Este proyecto está bajo la Licencia MIT - ver el archivo LICENSE para más detalles.


👤 Autor

Luis Aguilera Arteaga

Créditos


🌟 Star History

Si este proyecto te es útil, ¡considera darle una ⭐ en GitHub!


🇺🇸 English

LeyChile ePub Generator

Convert Chilean legislation from the National Congress Library into professional eBooks (ePub format).

This tool scrapes Chilean laws, decrees, and regulations from the official BCN (Biblioteca del Congreso Nacional) API and generates high-quality ePub files compatible with all major e-readers.

Quick Start

git clone https://github.com/laguileracl/leychile-epub.git
cd leychile-epub
python -m venv .venv && source .venv/bin/activate
pip install -e .
leychile-epub https://www.leychile.cl/Navegar?idNorma=242302

Features

  • 📚 Scrapes from official BCN XML API
  • 📱 Generates professional ePub files
  • 🎨 Premium styling and formatting
  • 📑 Interactive table of contents
  • 🔗 Cross-references between articles
  • 📇 Keyword index
  • ✅ Compatible with Kindle, Kobo, Apple Books, etc.

For full documentation, see the Spanish section above.


Made with ❤️ in Chile 🇨🇱