
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:
- What is XBRL
- Python Finance Library to Create XBRL Files
- Create XBRL File
- Add Schema Reference to XBRL
- Add Context in XBRL
- Create a Unit in XBRL
- Add Fact Item in XBRL
- Add Footnote Links to XBRL
- Insert Role Reference in XBRL
- 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:
- Instantiate XbrlDocument.
- Access the document’s xbrl_instances collection.
- Add a new XbrlInstance using
xbrl_instances.add(). - 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:
- Create an XbrlDocument object.
- Retrieve the xbrl_instances collection.
- Add a new instance with
xbrl_instances.add(). - Access the instance’s schema_refs collection.
- Insert a schema reference via
schema_refs.add(schema_path). - 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:
- Create an XbrlDocument.
- Add a new XBRL instance to xbrl_instances.
- Build a ContextPeriod with start and end dates.
- Create a ContextEntity containing the schema and identifier.
- Combine them into a Context object.
- Append the context to the instance’s contexts collection.
- 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:
- Instantiate XbrlDocument.
- Add a new instance to xbrl_instances.
- Create a Unit with
UnitTypeset to MEASURE. - Add qualified names to
measure_qualified_names. - Insert the unit into the instance’s units collection.
- 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:
- Create an XbrlDocument and a new instance.
- Add a schema reference and retrieve it.
- Create and add a Context.
- Define a Unit and add it.
- Obtain a Concept via
get_concept_by_name(). - Instantiate an Item with the concept.
- Set properties such as
context_ref,unit_ref,precision, andvalue. - Append the item to the facts collection.
- Save the XBRL file.
The code example below demonstrates adding a fact item.
Add Footnote Links to XBRL in Python
Footnote links connect facts to explanatory notes. Follow these steps:
- Create an XbrlDocument and a new instance.
- Add a schema reference and retrieve it.
- Create a Context and add it.
- Define a Footnote with a label and text.
- Create a Loc (locator) pointing to the target fact.
- Build a FootnoteArc linking the locator and footnote.
- Instantiate a FootnoteLink and add the footnote, locator, and arc.
- Add the FootnoteLink to the instance’s footnote_links collection.
- 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:
- Create an XbrlDocument and add a new instance.
- Add a schema reference and retrieve it.
- Obtain a RoleType using
get_role_type_by_uri(). - Create a RoleReference with the retrieved role type.
- Add the role reference to the role_references collection.
- 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:
- Create an XbrlDocument and a new instance.
- Add a schema reference and retrieve it.
- Get a RoleType via
get_arcrole_type_by_uri(). - Instantiate an ArcroleReference with the role type.
- Append it to the arcrole_references collection.
- 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.