使用 Python 创建 XBRL 文件

XBRL 文档是共同构成业务报告的事实的集合。我们可以轻松地以编程方式创建 XBRL 实例文档并添加对象或元素,例如模式引用、上下文、单元、项目等。在本文中,我们将学习如何使用 Python 创建 XBRL 文件。

本文将涵盖以下主题:

  1. 什么是 XBRL
  2. Python金融库创建XBRL文件
  3. 创建 XBRL 文件
  4. 向 XBRL 添加架构引用
  5. 在 XBRL 中添加上下文
  6. 在 XBRL 中创建一个单元
  7. 在 XBRL 中添加事实项目
  8. 添加脚注链接到 XBRL
  9. 在 XBRL 中插入角色参考
  10. 添加Arc角色引用到XBRL

什么是 XBRL

XBRL 代表可扩展业务报告语言,一种基于 XML 的标记语言。它用于公司财务业绩的标准化业务报告。它提供了一种在业务系统之间通信和交换业务信息的方法。

用于创建 XBRL 文件的 Python 财务库

要创建 XBRL 文件或实例文档,我们将使用 Aspose.Finance for Python API。它允许创建 XBRL 实例、解析和验证 XBRL 或 iXBRL 文件。

API 提供了 XbrlDocument 类,它表示包含一个或多个 XBRL 实例的 XBRL 文档。 XBRL 实例是一个 XML 片段,其根元素具有 XBRL 标记。 XbrlInstance 类提供了各种方法和属性来处理 XBRL 实例。 XbrlDocument 类的 save() 方法创建 XBRL 文件并将其保存到磁盘。请在 API 参考 中阅读有关使用 XBRL 对象的其他类的更多信息。

请在控制台中使用以下 pip 命令 下载包 或从 PyPI 安装 API:

pip install aspose-finance

在 Python 中创建 XBRL 实例文档

我们可以按照以下步骤轻松地从头开始创建 XBRL 实例文档:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 接下来,将 XbrlDocument 对象中的实例集合访问到 xbrlinstances 对象中。
  3. 之后,使用 xbrlinstances.add() 方法添加一个新的 XBRL 实例。
  4. 最后,使用 save(string) 方法保存 XBRL 文件。它将输出文件路径作为参数。

以下代码示例展示了如何使用 Python 创建 XBRL 实例。

# This code example demonstrates how to create an XBRL document
from aspose.finance.xbrl import XbrlDocument

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Save the file
xbrlDoc.save(outputFile)

创建 XBRL 实例后,我们可以在新创建的 XBRL 实例中添加模式引用、上下文、单位、项目、脚注链接、角色引用和弧形角色引用。

使用 Python 将模式引用添加到 XBRL

我们可以按照以下步骤在 XBRL 实例文档中添加模式引用:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 接下来,将 XbrlDocument 对象的实例集合放入 xbrlinstances 对象中。
  3. 然后,使用 xbrlinstances.add() 方法添加一个新的 XBRL 实例。
  4. 接下来,将 XbrlInstance 对象的架构引用集合放入 schemarefs 对象中。
  5. 之后,使用带有输入模式文件的 schemarefs.add() 方法添加新的模式引用。
  6. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何在 Python 中创建 XBRL 实例文档并添加架构引用。

# This code example demonstrates how to create an XBRL document and add schema reference object
from aspose.finance.xbrl import XbrlDocument

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"
inputSchemaFile = "C:\\Files\\schema.xsd"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Get instance schema references collection
schemaRefs = xbrlInstance.schema_refs

# Add schema
schemaRefs.add(inputSchemaFile, "example", "http://example.com/xbrl/taxonomy")

# Save the document
xbrlDoc.save(outputFile)

使用 Python 在 XBRL 中添加上下文

我们可以按照以下步骤向 XBRL 实例文档添加上下文:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 接下来,将新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 然后,使用开始日期和结束日期创建 ContextPeriod 类的实例。
  4. 接下来,使用架构和标识符创建一个 ContextEntity 对象。
  5. 然后,使用定义的 ContextPeriod 和 ContextEntity 创建 Context 类的实例。
  6. 之后,将 Context 对象添加到 contexts 集合中。
  7. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何在 Python 中创建 XBRL 实例文档并添加上下文对象。

# This code example demonstrates how to create an XBRL document and add Context object
from datetime import datetime
from aspose.finance.xbrl import XbrlDocument,ContextPeriod,ContextEntity,Context

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Add context period
contextPeriod = ContextPeriod(datetime(2020,1,1), datetime(2020,2,10))

# Add context entity
contextEntity = ContextEntity("exampleIdentifierScheme", "exampleIdentifier")

# Define and add context
context = Context(contextPeriod, contextEntity)
xbrlInstance.contexts.append(context)

# Save the document
xbrlDoc.save(outputFile)

使用 Python 在 XBRL 中创建单元

XBRL 中的单位测量数字项目。我们可以按照以下步骤在 XBRL 实例文档中添加一个单元:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 接下来,将一个新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 然后,使用 UnitType 作为 MEASURE 创建 Unit 类的实例。
  4. 接下来,将 QualifiedName 添加到 measurequalifiednames 集合中。
  5. 之后,将 Unit 添加到 units 集合中。
  6. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何在 Python 中创建 XBRL 文件并添加单元对象。

# This code example demonstrates how to create an XBRL document and add Unit object
from aspose.finance.xbrl import XbrlDocument,Unit,QualifiedName,UnitType

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Add Unit
unit = Unit(UnitType.MEASURE)
unit.measure_qualified_names.append(QualifiedName("USD", "iso4217", "http://www.xbrl.org/2003/iso4217"))
xbrlInstance.units.append(unit)

# Save the document
xbrlDoc.save(outputFile)

使用 Python 在 XBRL 中添加 Fact Item

XBRL 中的事实是使用项目元素定义的。 XBRL 中的项目包含简单事实的值和对正确解释该事实的上下文的引用。我们可以按照以下步骤在 XBRL 实例文档中添加项目:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 将新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 向 XbrlInstance 对象的 schemarefs 添加一个新的架构引用。
  4. 通过从 schemarefs 中的索引获取 SchemaRef。
  5. 初始化 Context 实例并将其添加到 contexts 集合中。
  6. 定义一个 Unit 实例并将其添加到单位集合中。
  7. 使用 getconceptbyname() 方法创建一个 Concept 类实例。
  8. 使用 Concept 对象作为参数创建 Item 类的实例。
  9. 设置项目属性,如 contextref、unitref、precision、value 等。
  10. 之后,将 Item 添加到事实集合中。
  11. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何使用 Python 在 XBRL 实例文档中添加事实作为项目元素。

# This code example demonstrates how to create an XBRL document and add Item object
from datetime import datetime
from aspose.finance.xbrl import XbrlDocument,ContextPeriod,ContextEntity,Context,Unit,UnitType,QualifiedName,Item

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"
inputSchemaFile = "C:\\Files\\schema.xsd"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Get instance schema references collection
schemaRefs = xbrlInstance.schema_refs

# Add schema
schemaRefs.add(inputSchemaFile, "example", "http://example.com/xbrl/taxonomy")
schema = schemaRefs[0]

# Add context
contextPeriod = ContextPeriod(datetime(2020,1,1), datetime(2020,2,10))
contextEntity = ContextEntity("exampleIdentifierScheme", "exampleIdentifier")
context = Context(contextPeriod, contextEntity)
xbrlInstance.contexts.append(context)

# Add unit
unit = Unit(UnitType.MEASURE)
unit.measure_qualified_names.append(QualifiedName("USD", "iso4217", "http://www.xbrl.org/2003/iso4217"))
xbrlInstance.units.append(unit)

# Add item
fixedAssetsConcept = schema.get_concept_by_name("fixedAssets")
if fixedAssetsConcept is not None:
    item = Item(fixedAssetsConcept)
    item.context_ref = context
    item.unit_ref = unit
    item.precision = 4
    item.value = "1444"
    xbrlInstance.facts.append(item)
    
# Save the document
xbrlDoc.save(outputFile)

我们可以按照以下步骤在 XBRL 实例文档中添加脚注链接:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 将新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 向 XbrlInstance 对象的 schemarefs 添加一个新的架构引用。
  4. 通过从 schemarefs 中的索引获取 SchemaRef。
  5. 初始化 Context 实例并将其添加到 contexts 集合中。
  6. 定义一个脚注类实例并设置它的标签和文本。
  7. 使用 Loc 类实例初始化 Locator 类型。
  8. 使用定位器标签和脚注标签作为参数定义 FootnoteArc 对象。
  9. 创建 FootnoteLink 类的实例。
  10. 将脚注、定位器和脚注弧添加到相关集合中。
  11. 之后,将脚注链接添加到脚注链接集合。
  12. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何使用 Python 在 XBRL 实例文档中添加脚注链接。

# This code example demonstrates how to create an XBRL document and add Footnote Link object
from datetime import datetime
from aspose.finance.xbrl import XbrlDocument,ContextPeriod,ContextEntity,Context,FootnoteLink,Footnote,Loc,FootnoteArc

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"
inputSchemaFile = "C:\\Files\\schema.xsd"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Get instance schema references collection
schemaRefs = xbrlInstance.schema_refs

# Add schema
schemaRefs.add(inputSchemaFile, "example", "http://example.com/xbrl/taxonomy")
schema = schemaRefs[0]

# Add context
contextPeriod = ContextPeriod(datetime(2020,1,1), datetime(2020,2,10))
contextEntity = ContextEntity("exampleIdentifierScheme", "exampleIdentifier")
context = Context(contextPeriod, contextEntity)
xbrlInstance.contexts.append(context)

# Add Footnote
footnote = Footnote("footnote1")
footnote.text = "Including the effects of the merger."

# Add Location
loc = Loc("#cd1", "fact1")

# Define Footnote arc
footnoteArc = FootnoteArc(loc.label, footnote.label)

# Add FootnoteLink
footnoteLink = FootnoteLink()
footnoteLink.footnotes.append(footnote)
footnoteLink.locators.append(loc)
footnoteLink.footnote_arcs.append(footnoteArc)
xbrlInstance.footnote_links.append(footnoteLink)
    
# Save the document
xbrlDoc.save(outputFile)

使用 Python 在 XBRL 中插入角色引用

我们可以按照以下步骤在 XBRL 实例文档中添加角色引用:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 将新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 向 XbrlInstance 对象的 schemarefs 添加一个新的架构引用。
  4. 通过从 schemarefs 中的索引获取 SchemaRef。
  5. 接下来,从 getroletypebyuri() 方法中获取 RoleType。
  6. 然后,使用 RoleType 对象作为参数创建 RoleReference 类的实例。
  7. 之后,将 RoleReference 添加到 rolereferences 集合中。
  8. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何使用 Python 在 XBRL 实例文档中添加角色引用。

# This code example demonstrates how to create an XBRL document and add Role reference object
from aspose.finance.xbrl import XbrlDocument,RoleReference

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"
inputSchemaFile = "C:\\Files\\schema.xsd"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Get instance schema references collection
schemaRefs = xbrlInstance.schema_refs

# Add schema
schemaRefs.add(inputSchemaFile, "example", "http://example.com/xbrl/taxonomy")
schema = schemaRefs[0]

# Get role type
roleType = schema.get_role_type_by_uri("http://abc.com/role/link1")

# Add role reference
if roleType is not None:
    roleReference = RoleReference(roleType)
    xbrlInstance.role_references.append(roleReference)

# Save the document
xbrlDoc.save(outputFile)

在 Python 中添加对 XBRL 的 Arc 角色引用

我们可以按照以下步骤在 XBRL 实例文档中添加 Arc 角色引用:

  1. 首先,创建 XbrlDocument 类的实例。
  2. 将新的 XBRL 实例添加到 XbrlDocument 对象的 xbrlinstances。
  3. 向 XbrlInstance 对象的 schemarefs 添加一个新的架构引用。
  4. 通过从 schemarefs 中的索引获取 SchemaRef。
  5. 接下来,从 getarcroletypebyuri() 方法中获取 RoleType。
  6. 然后,使用 RoleType 对象作为参数创建 ArcroleReference 类的实例。
  7. 之后,将 ArcroleReference 添加到 arcrolereferences 集合中。
  8. 最后,使用 save(string) 方法保存 XBRL 文件。

以下代码示例展示了如何使用 Python 在 XBRL 实例文档中添加弧角色引用。

# This code example demonstrates how to create an XBRL document and add Arc Role reference object
from aspose.finance.xbrl import XbrlDocument,ArcroleReference

# The path to the documents directory.
outputFile = "C:\\Files\\document.xbrl"
inputSchemaFile = "C:\\Files\\schema.xsd"

# Create XBRL document
xbrlDoc = XbrlDocument()

# Get document instances collection
xbrlInstances = xbrlDoc.xbrl_instances

# Add instance to the collection
xbrlInstance = xbrlInstances[xbrlInstances.add()]

# Get instance schema references collection
schemaRefs = xbrlInstance.schema_refs

# Add schema
schemaRefs.add(inputSchemaFile, "example", "http://example.com/xbrl/taxonomy")
schema = schemaRefs[0]

# Get role type
roleType = schema.get_role_type_by_uri("http://abc.com/role/link1")

# Add role reference
if roleType is not None:
    arcroleReference = ArcroleReference(roleType)
    xbrlInstance.arcrole_references.append(arcroleReference)

# Save the document
xbrlDoc.save(outputFile)

获得免费许可证

您可以 获得免费的临时许可证 试用该库而不受评估限制。

结论

在本文中,我们学习了如何使用 Python 创建 XBRL 文档。我们还看到了如何以编程方式将各种 XBRL 对象添加到创建的 XBRL 实例文档中。此外,您可以使用 documentation 了解更多关于 Aspose.Finance for Python API 的信息。如有任何歧义,请随时在我们的 论坛 上与我们联系。

也可以看看