Create XBRL File using Python

An XBRL document is a structured collection of business facts used for financial reporting. With Aspose.Finance for Python you can generate XBRL instance documents programmatically and enrich them with schema references, contexts, units, fact items, footnotes, and role definitions. This article demonstrates how to create and populate an XBRL file using Python.

The following topics are covered:

  1. What is XBRL
  2. Python Finance Library to Create XBRL Files
  3. Create XBRL File
  4. Add Schema Reference to XBRL
  5. Add Context in XBRL
  6. Create a Unit in XBRL
  7. Add Fact Item in XBRL
  8. Add Footnote Links to XBRL
  9. Insert Role Reference in XBRL
  10. Add Arc Role Reference to XBRL

What is XBRL

XBRL (eXtensible Business Reporting Language) is an XML‑based standard that enables the exchange of financial statements and business reports in a machine‑readable format. It provides a consistent way to represent facts, contexts, and units, allowing regulators, analysts, and investors to process data automatically.

Python Finance Library to Create XBRL Files

To generate XBRL files we use the Aspose.Finance for Python API. This library simplifies XBRL creation, parsing, and validation. The core classes are:

  • XbrlDocument – Represents an XBRL document that can contain one or more instances.
  • XbrlInstance – Provides methods to add schema references, contexts, units, facts, and linkbase elements.
  • save() – Persists the XBRL document to disk.

Explore additional classes in the API reference for deeper integration.

Install the package from PyPI:

pip install aspose-finance

Create XBRL Instance Document in Python

Follow these steps to build a fresh XBRL instance:

  1. Instantiate XbrlDocument.
  2. Access the document’s xbrl_instances collection.
  3. Add a new XbrlInstance using xbrl_instances.add().
  4. Save the document with save(output_path).

The code snippet below demonstrates the complete process.

After creating the instance you can attach schema references, contexts, units, facts, footnotes, role references, and arc role references.

Add Schema Reference to XBRL using Python

A schema reference links the XBRL instance to an external taxonomy. Add it as follows:

  1. Create an XbrlDocument object.
  2. Retrieve the xbrl_instances collection.
  3. Add a new instance with xbrl_instances.add().
  4. Access the instance’s schema_refs collection.
  5. Insert a schema reference via schema_refs.add(schema_path).
  6. Save the document.

The example below shows how to add a schema reference.

Add Context in XBRL using Python

Contexts define the entity and reporting period for facts. To add a context:

  1. Create an XbrlDocument.
  2. Add a new XBRL instance to xbrl_instances.
  3. Build a ContextPeriod with start and end dates.
  4. Create a ContextEntity containing the schema and identifier.
  5. Combine them into a Context object.
  6. Append the context to the instance’s contexts collection.
  7. Save the file.

The code sample illustrates these steps.

Create Units in XBRL using Python

Units describe the measurement system for numeric facts. To create a unit:

  1. Instantiate XbrlDocument.
  2. Add a new instance to xbrl_instances.
  3. Create a Unit with UnitType set to MEASURE.
  4. Add qualified names to measure_qualified_names.
  5. Insert the unit into the instance’s units collection.
  6. Save the document.

The following snippet adds a unit to an XBRL file.

Add Fact Item in XBRL using Python

Fact items represent the actual data points. To add an item:

  1. Create an XbrlDocument and a new instance.
  2. Add a schema reference and retrieve it.
  3. Create and add a Context.
  4. Define a Unit and add it.
  5. Obtain a Concept via get_concept_by_name().
  6. Instantiate an Item with the concept.
  7. Set properties such as context_ref, unit_ref, precision, and value.
  8. Append the item to the facts collection.
  9. Save the XBRL file.

The code example below demonstrates adding a fact item.

Footnote links connect facts to explanatory notes. Follow these steps:

  1. Create an XbrlDocument and a new instance.
  2. Add a schema reference and retrieve it.
  3. Create a Context and add it.
  4. Define a Footnote with a label and text.
  5. Create a Loc (locator) pointing to the target fact.
  6. Build a FootnoteArc linking the locator and footnote.
  7. Instantiate a FootnoteLink and add the footnote, locator, and arc.
  8. Add the FootnoteLink to the instance’s footnote_links collection.
  9. Save the document.

The snippet below shows the implementation.

Insert Role Reference in XBRL using Python

Role references define custom link roles. To insert one:

  1. Create an XbrlDocument and add a new instance.
  2. Add a schema reference and retrieve it.
  3. Obtain a RoleType using get_role_type_by_uri().
  4. Create a RoleReference with the retrieved role type.
  5. Add the role reference to the role_references collection.
  6. Save the XBRL file.

The following code adds a role reference.

Add Arc Role Reference to XBRL in Python

Arc role references describe the semantics of arcs in linkbases. To add one:

  1. Create an XbrlDocument and a new instance.
  2. Add a schema reference and retrieve it.
  3. Get a RoleType via get_arcrole_type_by_uri().
  4. Instantiate an ArcroleReference with the role type.
  5. Append it to the arcrole_references collection.
  6. Save the document.

The code sample below illustrates this process.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

This guide has shown how to create a complete XBRL document using Python and how to enrich it with schema references, contexts, units, fact items, footnotes, role references, and arc role references. For deeper details, explore the Aspose.Finance for Python documentation at the official docs. If you have questions, visit our forum for support.

See Also