Skip to main content

A flexible XML to document converter with dynamic element processing and plugin system

Project description

XMLCV

A flexible Python library for converting XML files to various document formats with dynamic element processing and plugin system.

Features

  • 🔄 Dynamic Element Processing: Automatically analyzes XML structure and processes elements accordingly
  • 📄 Multiple Format Support: Handles various XML schemas by loading element definitions from XML
  • 🎨 Customizable Styling: Support for custom CSS and styling options
  • 📑 Table of Contents: Automatic TOC generation
  • 📊 PDF Export: Optional PDF conversion using WeasyPrint or pdfkit
  • 🔧 Extensible: Easy to add custom element processors

Installation

Basic Installation

pip install xmlcv

Optional Dependencies

The package has optional dependencies for different output formats. You can install them individually or all at once:

# Install specific formats
pip install 'xmlcv[pdf]'      # PDF support
pip install 'xmlcv[docx]'      # DOCX support
pip install 'xmlcv[pptx]'    # PPTX support
pip install 'xmlcv[markdown]' # Markdown support

# Install multiple formats
pip install 'xmlcv[pdf,docx]'

# Install all formats
pip install 'xmlcv[all]'

List Available Plugins

xmlcv --list-plugins

Quick Start

Command Line

# List available output formats
xmlcv --list-plugins

# Convert single file to HTML (default)
xmlcv input.xml -o output.html

# Convert to different formats
xmlcv input.xml --format pdf -o output.pdf
xmlcv input.xml --format docx -o output.docx

# Convert directory
xmlcv input_dir/ -o output_dir/

# Analyze XML structure
xmlcv input.xml --analyze

Python API

Using XMLConverter (New Plugin System)

from xmlcv import XMLConverter, ConverterConfig
from pathlib import Path

# Basic usage with plugin system
converter = XMLConverter()

# List available plugins
plugins = converter.list_plugins()
for plugin in plugins:
    print(f"{plugin.name}: {plugin.description}")

# Convert to different formats
converter.convert(Path("input.xml"), Path("output.html"), output_format="html")
converter.convert(Path("input.xml"), Path("output.pdf"), output_format="pdf")

# Convert to multiple formats at once
converter.convert_to_multiple_formats(
    Path("input.xml"),
    output_formats=["html", "pdf"]
)

Using XMLToHTMLConverter (Legacy API)

from xmlcv import XMLToHTMLConverter, ConverterConfig
from pathlib import Path

# Basic usage
converter = XMLToHTMLConverter()
html = converter.convert_file(
    Path("input.xml"),
    Path("output.html")
)

# With custom configuration
config = ConverterConfig(
    convert_to_pdf=True,
    include_navigation=True,
    custom_css="/* your custom CSS */"
)
converter = XMLToHTMLConverter(config)
html = converter.convert_file(Path("input.xml"), Path("output.html"))

# Convert entire directory
converter.convert_directory(
    Path("xml_files/"),
    Path("html_output/")
)

Advanced Usage

Custom Element Processors

from xmlcv import XMLToHTMLConverter, ConverterConfig
import xml.etree.ElementTree as ET

def custom_processor(element: ET.Element, context: dict) -> str:
    """Custom processor for a specific element"""
    text = element.text or ""
    return f'<div class="custom">{text}</div>'

# Register custom processor
config = ConverterConfig()
config.element_processors['CustomElement'] = custom_processor

converter = XMLToHTMLConverter(config)

Analyzing XML Structure

from xmlcv import XMLToHTMLConverter
from pathlib import Path

converter = XMLToHTMLConverter()
structure = converter.analyze_xml(Path("input.xml"))

print(f"Elements found: {structure['elements']}")
print(f"Hierarchy: {structure['hierarchy']}")

Supported Elements

The converter dynamically supports various XML elements including:

  • Structure: Document, Title, Body, MainContent
  • Organization: Chapter, Section, Subsection, Article
  • Content: Paragraph, Sentence, Item, Subitem1, Subitem2
  • Tables: TableStruct, Table, TableRow, TableColumn
  • Special: Statement, Provision, Appendix, TOC
  • Formatting: Ruby (annotations), Sub (subscript), and more

Configuration

ConverterConfig Options

  • output_dir: Output directory path
  • create_index: Create index.html page
  • convert_to_pdf: Enable PDF conversion
  • css_file: Path to custom CSS file
  • custom_css: Custom CSS string
  • include_toc: Include table of contents
  • include_navigation: Include navigation sidebar
  • element_processors: Dictionary of custom element processors
  • pdf_options: PDF generation options

Requirements

  • Python 3.8+
  • lxml >= 4.9.0

Optional:

  • weasyprint >= 59.0 (for PDF export)
  • pdfkit >= 1.0.0 (alternative PDF export)

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

0.1.0

  • Initial release
  • Dynamic element processing
  • Support for various XML structures
  • PDF export support
  • CLI interface

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

xmlcv-0.1.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmlcv-0.1.0-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file xmlcv-0.1.0.tar.gz.

File metadata

  • Download URL: xmlcv-0.1.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xmlcv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3896f0fbf71bbc34def9d2b1e39b6be0611910d1e41120a8fbc61b4cea63fa91
MD5 9652343a24e82f1eb9cd1d340aac4e5c
BLAKE2b-256 9c77e10bfcbaeaa67ca56b494c00a05b9b357621f22dcdb0010f9a47b4599173

See more details on using hashes here.

Provenance

The following attestation bundles were made for xmlcv-0.1.0.tar.gz:

Publisher: publish.yml on trunghiahoang/xmlcv

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xmlcv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xmlcv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xmlcv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04dccd2c6147349bc7912b4863257437d570753f12dedb211fa2287e1eb945e2
MD5 ec1fa455e9be756f2c086846f0f2333e
BLAKE2b-256 2d4f32474060abfe703e68421c9ea1f19065fb7c1703229aac7e6fdca56ffad5

See more details on using hashes here.

Provenance

The following attestation bundles were made for xmlcv-0.1.0-py3-none-any.whl:

Publisher: publish.yml on trunghiahoang/xmlcv

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page