Generación de XML UBL 2.1 para Facturación Electrónica Perú
Project description
xfep-xml
Generación de XML UBL 2.1 para Facturación Electrónica Perú (SUNAT).
Parte del ecosistema XFEP. Depende de xfep-models.
Instalación
pip install xfep-xml
Uso
from xfep.xml import XmlBuilder
from xfep.models import Invoice, Client, Detalle, Company
company = Company(
ruc="20123456789",
razon_social="MI EMPRESA S.A.C.",
nombre_comercial="MI EMPRESA",
direccion="Av. Principal 123",
ubigeo="150101",
distrito="Lima",
provincia="Lima",
departamento="Lima",
usuario_sol="MODDATOS",
clave_sol="MODDATOS",
)
invoice = Invoice(
company_id=1,
branch_id=1,
serie="F001",
fecha_emision="2026-02-10",
moneda="PEN",
tipo_operacion="0101",
forma_pago_tipo="Contado",
client=Client(
tipo_documento="6",
numero_documento="20987654321",
razon_social="CLIENTE S.A.C.",
),
detalles=[
Detalle(
codigo="PROD001",
descripcion="Laptop HP",
unidad="NIU",
cantidad=1,
mto_precio_unitario=2360,
porcentaje_igv=18,
tip_afe_igv="10",
)
],
)
builder = XmlBuilder()
xml_bytes = builder.build(invoice, company)
# xml_bytes es UTF-8 encoded, listo para firmar con xfep-sign
Documentos soportados
| Documento | Modelo | Template | Root XML |
|---|---|---|---|
| Factura | Invoice |
invoice.xml.j2 |
<Invoice> |
| Boleta | Boleta |
invoice.xml.j2 (compartido) |
<Invoice> |
| Nota de Crédito | CreditNote |
credit_note.xml.j2 |
<CreditNote> |
| Nota de Débito | DebitNote |
debit_note.xml.j2 |
<DebitNote> |
| Comunicación de Baja | VoidedDocument |
voided.xml.j2 |
<VoidedDocuments> |
| Resumen Diario | DailySummary |
summary.xml.j2 |
<SummaryDocuments> |
| Guía de Remisión | DispatchGuide |
dispatch.xml.j2 |
<DespatchAdvice> |
API
XmlBuilder
from xfep.xml import XmlBuilder
builder = XmlBuilder()
# Generar XML para cualquier documento SUNAT
xml_bytes: bytes = builder.build(document, company)
build(document, company) -> bytes
document— Instancia de cualquier modelo SUNAT (Invoice,Boleta,CreditNote, etc.)company— Instancia deCompany(datos del emisor)- Retorna XML como
bytes(UTF-8 con declaración XML) - Lanza
ValueErrorsi el tipo de documento no es soportado
Características
- UBL 2.1 compliant — Namespaces correctos (cac, cbc, ext, ds, sac)
- Declaración XML —
<?xml version="1.0" encoding="UTF-8"?> - Placeholder de firma —
ext:UBLExtensionscon slot vacío parads:Signature(firma real en xfep-sign) - Cálculo automático — Subtotales, IGV, totales por línea y documento
- Agrupación de impuestos — TaxSubtotal agrupado por tipo (IGV=1000, ISC=2000, IVAP=1016, ICBPER=7152)
- Precisión decimal — Montos a 2 decimales, cantidades con precisión completa
- Validación lxml — XML generado es parseado con lxml para garantizar well-formedness
Namespaces UBL 2.1
| Prefijo | URI |
|---|---|
| (default) | urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 |
cac |
urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2 |
cbc |
urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2 |
ext |
urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2 |
ds |
http://www.w3.org/2000/09/xmldsig# |
sac |
urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1 |
Desarrollo
git clone https://github.com/xpertik/xfep-xml.git
cd xfep-xml
python3.13 -m venv .venv
source .venv/bin/activate
# Instalar xfep-models desde local (desarrollo)
pip install -e "../xfep-models"
pip install -e ".[dev]"
pytest -v
Stack
- Python >= 3.13
- Jinja2 >= 3.1 (templates XML)
- lxml >= 5.0 (parsing/validación)
- xfep-models >= 0.1.0
- Build: Hatchling
- Tests: pytest
Parte del ecosistema XFEP
| Paquete | Estado | Descripción |
|---|---|---|
| xfep-models | v0.1.0 | Modelos de datos |
| xfep-xml | v0.1.0 | Generación de XML UBL 2.1 |
| xfep-sign | pendiente | Firma digital XML |
| xfep-ws | pendiente | Cliente SOAP/REST para SUNAT |
| xfep-pdf | pendiente | Generación de PDF |
| xfep-parser | pendiente | Parseo de respuestas SUNAT |
Licencia
Apache License 2.0 — ver LICENSE.
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
xfep_xml-0.1.0.tar.gz
(36.9 kB
view details)
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
xfep_xml-0.1.0-py3-none-any.whl
(21.1 kB
view details)
File details
Details for the file xfep_xml-0.1.0.tar.gz.
File metadata
- Download URL: xfep_xml-0.1.0.tar.gz
- Upload date:
- Size: 36.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29414330554197a637fa22dc9c4f0951c620e82dd1dc54da054d5f3c7ac3afdf
|
|
| MD5 |
3c083bc9892fa8f05f0ed58b20d6e17d
|
|
| BLAKE2b-256 |
70a35731836b2964665c2fef65db3f7899ecdf53066078c52e48e15e09fc09f1
|
File details
Details for the file xfep_xml-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xfep_xml-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bddbdfcd5a6145c2a168007b5afdbc3d88e78f6d44a31ce5e2c05233fd9ca585
|
|
| MD5 |
02ae99cadaaff2997a6620e8722f1260
|
|
| BLAKE2b-256 |
07c47598108b2ea487fa1b72400cf9d7bb6f4883cbba4cf66a3035385efa8bdb
|