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:
- 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 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:
- Firstly, create an instance of the XbrlDocument class.
- Next, access instances’ collection from the XbrlDocument object into the xbrl_instances object.
- After that, add a new XBRL instance using the xbrl_instances.add() method.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Next, get the XbrlDocument object’s instances’ collection into the xbrl_instances object.
- Then, add a new XBRL instance using the xbrl_instances.add() method.
- Next, get the XbrlInstance object’s schema reference collection into the schema_refs object.
- After that, add a new schema reference using schema_refs.add() method with input schema file.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Next, Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Then, create an instance of the ContextPeriod class with the start and end dates.
- Next, create a ContextEntity object with schema and identifier.
- Then, create an instance of the Context class with the defined ContextPeriod and ContextEntity.
- After that, add the Context object to the contexts collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Next, add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Then, create an instance of the Unit class with the UnitType as MEASURE.
- Next, add QualifiedName to the measure_qualified_names collection.
- After that, add the Unit to the units collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Add a new schema reference to the XbrlInstance object’s schema_refs.
- Get SchemaRef by its index from schema_refs.
- Initialize the Context instance and add it to the contexts collection.
- Define a Unit instance and add it to the units collection.
- Create a Concept class instance using the get_concept_by_name() method.
- Create an instance of the Item class with the Concept object as an argument.
- Set item properties such as context_ref, unit_ref, precision, value, etc.
- After that, add the Item to the facts collection.
- 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.
Add Footnote Links to XBRL in Python
We can add a footnote link in an XBRL instance document by following the steps given below:
- Firstly, create an instance of the XbrlDocument class.
- Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Add a new schema reference to the XbrlInstance object’s schema_refs.
- Get SchemaRef by its index from schema_refs.
- Initialize the Context instance and add it to the contexts collection.
- Define a Footnote class instance and set its label and text.
- Initialize Locator type using the Loc class instance.
- Define FootnoteArc object with Locator label and Footnote label as arguments.
- Create an instance of the FootnoteLink class.
- Add Footnote, Locator, and FootnoteArc to relevant collections.
- After that, add the FootnoteLink to the footnote_links collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Add a new schema reference to the XbrlInstance object’s schema_refs.
- Get SchemaRef by its index from schema_refs.
- Next, get the RoleType from the get_role_type_by_uri() method.
- Then, create an instance of the RoleReference class with the RoleType object as an argument.
- After that, add the RoleReference to the role_references collection.
- 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:
- Firstly, create an instance of the XbrlDocument class.
- Add a new XBRL instance to the XbrlDocument object’s xbrl_instances.
- Add a new schema reference to the XbrlInstance object’s schema_refs.
- Get SchemaRef by its index from schema_refs.
- Next, get the RoleType from the get_arcrole_type_by_uri() method.
- Then, create an instance of the ArcroleReference class with the RoleType object as an argument.
- After that, add the ArcroleReference to the arcrole_references collection.
- 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.