Skip to main content

parse and generate Italian Fattura Elettronica

Project description

# Python A38

Library to generate Italian Fattura Elettronica from Python.

This library implements a declarative data model similar to Django models, that
is designed to describe, validate, serialize and parse Italian Fattura
Elettronica data.

Only part of the specification is implemented, with more added as needs will
arise. You are welcome to implement the missing pieces you need and send a pull
request: the idea is to have a good, free (as in freedom) library to make
billing in Italy with Python easier for everyone.

The library can generate various kinds of fatture that pass validation, and can
parse all the example XML files distributed by

## Dependencies

Required: dateutil, pytz, asn1crypto, and the python3 standard library.

* yapf for formatting `a38tool python` output
* lxml for rendering to HTML
* the wkhtmltopdf command for rendering to PDF
* requests for downloading CA certificates for signature verification

## `a38tool` script

A simple command line wrapper to the library functions is available as `a38tool`:

$ a38tool --help
usage: a38tool [-h] [--verbose] [--debug]
{json,xml,python,diff,validate,html,pdf,update_capath} ...

Handle fattura elettronica files

positional arguments:
json output a fattura in JSON
xml output a fattura in XML
python output a fattura as Python code
diff show the difference between two fatture
validate validate the contents of a fattura
html render a Fattura as HTML using a .xslt stylesheet
pdf render a Fattura as PDF using a .xslt stylesheet
update_capath create/update an openssl CApath with CA certificates
that can be used to validate digital signatures

optional arguments:
-h, --help show this help message and exit
--verbose, -v verbose output
--debug debug output

See []( for more details.

## Example code

import a38.fattura as a38
import datetime

cedente_prestatore = a38.CedentePrestatore(
a38.IdFiscaleIVA("IT", "01234567890"),
anagrafica=a38.Anagrafica(denominazione="Test User"),
a38.Sede(indirizzo="via Monferrato", numero_civico="1", cap="50100", comune="Firenze", provincia="FI", nazione="IT"),

cessionario_committente = a38.CessionarioCommittente(
a38.IdFiscaleIVA("IT", "76543210987"),
anagrafica=a38.Anagrafica(denominazione="A Company SRL"),
a38.Sede(indirizzo="via Langhe", numero_civico="1", cap="50142", comune="Firenze", provincia="FI", nazione="IT"),

bill_number = 1

f = a38.FatturaPrivati12()
f.fattura_elettronica_header.dati_trasmissione = a38.DatiTrasmissione(
a38.IdTrasmittente("IT", "10293847561"),
f.fattura_elettronica_header.cedente_prestatore = cedente_prestatore
f.fattura_elettronica_header.cessionario_committente = cessionario_committente

body = a38.FatturaElettronicaBody()
body.dati_generali.dati_generali_documento = a38.DatiGeneraliDocumento(
causale=["Test billing"],

descrizione="Test item", quantita=2, unita_misura="kg",
prezzo_unitario="25.50", aliquota_iva="22.00")

descrizione="Other item", quantita=1, unita_misura="kg",
prezzo_unitario="15.50", aliquota_iva="22.00")




filename = "{}{}_{:05d}.xml".format(

tree = f.build_etree()
with open(filename, "wb") as out:

# Digital signatures

Digital signatures on Firma Elettronica are
[CAdES]( signatures.

openssl cal verify the signatures, but not yet generate them. A patch to sign
with CAdES [has been recently merged](
but not yet released as of 2019-02-26.

## Downloading CA certificates

CA certificates for validating digital certificates are
[distributed by the EU in XML format](
See also [the AGID page about it](

There is a [Trusted List Browser]( but
apparently no way of getting a simple bundle of certificates useable by

`a38tool` has basic features to download and parse CA certificate information,
and maintain a CA certificate directory:

a38tool update_capath certdir/ --remove-old

No particular effort is made to validate the downloaded certificates, besides
the standard HTTPS checks performed by the [requests

## Verifying signed `.p7m` files

Once you have a CA certificate directory, verifying signed p7m files is quite

openssl cms -verify -in tests/data/test.txt.p7m -inform der -CApath certs/

# Useful links

XSLT stylesheets for displaying fatture:

* From for
* From [AssoSoftware](

# Copyright

Copyright 2019 Truelite S.r.l.

This software is released under the Apache License 2.0

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for a38, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size a38-0.1.1-py3-none-any.whl (32.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size a38-0.1.1.tar.gz (40.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page