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 pathcreate_index: Create index.html pageconvert_to_pdf: Enable PDF conversioncss_file: Path to custom CSS filecustom_css: Custom CSS stringinclude_toc: Include table of contentsinclude_navigation: Include navigation sidebarelement_processors: Dictionary of custom element processorspdf_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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3896f0fbf71bbc34def9d2b1e39b6be0611910d1e41120a8fbc61b4cea63fa91
|
|
| MD5 |
9652343a24e82f1eb9cd1d340aac4e5c
|
|
| BLAKE2b-256 |
9c77e10bfcbaeaa67ca56b494c00a05b9b357621f22dcdb0010f9a47b4599173
|
Provenance
The following attestation bundles were made for xmlcv-0.1.0.tar.gz:
Publisher:
publish.yml on trunghiahoang/xmlcv
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xmlcv-0.1.0.tar.gz -
Subject digest:
3896f0fbf71bbc34def9d2b1e39b6be0611910d1e41120a8fbc61b4cea63fa91 - Sigstore transparency entry: 779867337
- Sigstore integration time:
-
Permalink:
trunghiahoang/xmlcv@1874d5e0009f5e26572b4fab70f35fd2fae39f95 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/trunghiahoang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1874d5e0009f5e26572b4fab70f35fd2fae39f95 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04dccd2c6147349bc7912b4863257437d570753f12dedb211fa2287e1eb945e2
|
|
| MD5 |
ec1fa455e9be756f2c086846f0f2333e
|
|
| BLAKE2b-256 |
2d4f32474060abfe703e68421c9ea1f19065fb7c1703229aac7e6fdca56ffad5
|
Provenance
The following attestation bundles were made for xmlcv-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on trunghiahoang/xmlcv
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xmlcv-0.1.0-py3-none-any.whl -
Subject digest:
04dccd2c6147349bc7912b4863257437d570753f12dedb211fa2287e1eb945e2 - Sigstore transparency entry: 779867338
- Sigstore integration time:
-
Permalink:
trunghiahoang/xmlcv@1874d5e0009f5e26572b4fab70f35fd2fae39f95 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/trunghiahoang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1874d5e0009f5e26572b4fab70f35fd2fae39f95 -
Trigger Event:
workflow_dispatch
-
Statement type: