Create XBRL File using Python

An XBRL document is a collection of facts that together make up a business report. We can easily create an XBRL instance document programmatically and add objects or elements such as schema reference, context, unit, items, etc. In this article, we will learn how to create an XBRL file using Python.

The following topics shall be covered in this article:

  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 stands for eXtensible Business Reporting Language, an XML-based markup language. It is used for standardized business reporting of a company’s financial performance. It provides a way to communicate and exchange business information between business systems.

Python Finance Library to Create XBRL Files

To create an XBRL file or instance document, we will be using the Aspose.Finance for Python API. It allows creating XBRL instances, parsing, and validating the XBRL or iXBRL files.

The API provides the XbrlDocument class that represents an XBRL document containing one or more XBRL instances. An XBRL instance is an XML fragment, with the root element having an XBRL tag. The XbrlInstance class provides various methods and properties to work with XBRL instances. The save() method of the XbrlDocument class creates and saves the XBRL file to the disk. Please read more about other classes to work with XBRL objects in API reference.

Please either download the package or install the API from PyPI using the following pip command in the console:

pip install aspose-finance

Create XBRL Instance Document in Python

We can easily create an XBRL instance document from scratch by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, access instances’ collection from the XbrlDocument object into the xbrl_instances object.
  3. After that, add a new XBRL instance using the xbrl_instances.add() method.
  4. Finally, save the XBRL file using the save(string) method. It takes the output file path as an argument.

The following code sample shows how to create an XBRL instance using Python.

Once we have created the instance of XBRL, we can add the schema reference, context, units, items, footnote links, role references, and arc role references in the newly created XBRL instance.

Add Schema Reference to XBRL using Python

We can add schema reference in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, get the XbrlDocument object’s instances’ collection into the xbrl_instances object.
  3. Then, add a new XBRL instance using the xbrl_instances.add() method.
  4. Next, get the XbrlInstance object’s schema reference collection into the schema_refs object.
  5. After that, add a new schema reference using schema_refs.add() method with input schema file.
  6. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to create an XBRL instance document and add schema reference in Python.

Add Context in XBRL using Python

We can add a context to an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Then, create an instance of the ContextPeriod class with the start and end dates.
  4. Next, create a ContextEntity object with schema and identifier.
  5. Then, create an instance of the Context class with the defined ContextPeriod and ContextEntity.
  6. After that, add the Context object to the contexts collection.
  7. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to create an XBRL instance document and add a context object in Python.

Create Units in XBRL using Python

The units in XBRL measure numeric Items. We can add a unit in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Next, add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Then, create an instance of the Unit class with the UnitType as MEASURE.
  4. Next, add QualifiedName to the measure_qualified_names collection.
  5. After that, add the Unit to the units collection.
  6. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to create an XBRL file and add a unit object in Python.

Add Fact Item in XBRL using Python

Facts in XBRL are defined using item elements. An item in XBRL contains the value of the simple fact and a reference to the context to correctly interpret that fact. We can add an item in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Add a new schema reference to the XbrlInstance object’s schema_refs.
  4. Get SchemaRef by its index from schema_refs.
  5. Initialize the Context instance and add it to the contexts collection.
  6. Define a Unit instance and add it to the units collection.
  7. Create a Concept class instance using the get_concept_by_name() method.
  8. Create an instance of the Item class with the Concept object as an argument.
  9. Set item properties such as context_ref, unit_ref, precision, value, etc.
  10. After that, add the Item to the facts collection.
  11. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to add a fact as an item element in an XBRL instance document using Python.

We can add a footnote link in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Add a new schema reference to the XbrlInstance object’s schema_refs.
  4. Get SchemaRef by its index from schema_refs.
  5. Initialize the Context instance and add it to the contexts collection.
  6. Define a Footnote class instance and set its label and text.
  7. Initialize Locator type using the Loc class instance.
  8. Define FootnoteArc object with Locator label and Footnote label as arguments.
  9. Create an instance of the FootnoteLink class.
  10. Add Footnote, Locator, and FootnoteArc to relevant collections.
  11. After that, add the FootnoteLink to the footnote_links collection.
  12. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to add a footnote link in an XBRL instance document using Python.

Insert Role Reference in XBRL using Python

We can add a Role reference in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Add a new schema reference to the XbrlInstance object’s schema_refs.
  4. Get SchemaRef by its index from schema_refs.
  5. Next, get the RoleType from the get_role_type_by_uri() method.
  6. Then, create an instance of the RoleReference class with the RoleType object as an argument.
  7. After that, add the RoleReference to the role_references collection.
  8. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to add a role reference in an XBRL instance document using Python.

Add Arc Role Reference to XBRL in Python

We can add an Arc role reference in an XBRL instance document by following the steps given below:

  1. Firstly, create an instance of the XbrlDocument class.
  2. Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
  3. Add a new schema reference to the XbrlInstance object’s schema_refs.
  4. Get SchemaRef by its index from schema_refs.
  5. Next, get the RoleType from the get_arcrole_type_by_uri() method.
  6. Then, create an instance of the ArcroleReference class with the RoleType object as an argument.
  7. After that, add the ArcroleReference to the arcrole_references collection.
  8. Finally, save the XBRL file using the save(string) method.

The following code sample shows how to add an arc role reference in an XBRL instance document using Python.

Get a Free License

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

Conclusion

In this article, we have learned how to create an XBRL document using Python. We have also seen how to add various XBRL objects to the created XBRL instance document programmatically. Besides, you can learn more about Aspose.Finance for Python API using the documentation. In case of any ambiguity, please feel free to contact us on our forum.

See Also