跳转到主要内容

Python库,用于从XML文档生成巴西辅助财政文件PDF。

项目描述

image image image image image beta

巴西财政报告

Python库,用于从XML文档生成巴西辅助财政文件PDF。

支持文档 📄

  • DANFE - 电子发票辅助文件 (NF-e)
  • DACCe - 电子校正函辅助文件 (CC-e )

Beta 版本通知 🚧

这个库目前处于开发的 Beta 阶段。虽然已经实现了许多预期功能,但仍处于测试和改进中。用户请注意,在此阶段,功能可能会改变,并可能发生一些不稳定。我们欢迎对任何问题或改进建议的反馈。在生产环境中使用应谨慎对待。

依赖项 🛠️

  • FPDF2 - Python 的 PDF 创建库
  • phonenumbers
  • python-barcode

安装 🔧

pip install brazilfiscalreport

使用示例 🚀

DANFE

from brazilfiscalreport.danfe import Danfe

# Path to the XML file
xml_file_path = 'nfe.xml'

# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
    xml_content = file.read()

# Instantiate the DANFE object with the loaded XML content
danfe = Danfe(xml=xml_content)

# Save the generated PDF to a file
danfe.output('danfe.pdf')

DACCe

from brazilfiscalreport.dacce import DaCCe

# Path to the XML file
xml_file_path = 'cce.xml'

# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
    xml_content = file.read()

# Instantiate the CC-e PDF object with the loaded XML content
cce = DaCCe(xml=xml_content)

# Save the generated PDF to a file
cce.output('cce.pdf')

示例 📝

一些由我们的单元测试生成的示例 PDF 可在 tests/generated 目录中查看。

自定义 DANFE 🎨

本节介绍了如何使用 DanfeConfig 类自定义 DANFE 的 PDF 输出。您可以根据需要调整各种设置,如边距、字体和税配置。

配置选项 ⚙️

以下是 DanfeConfig 中所有可用配置选项的说明

  1. Logo

    • 类型: strBytesIObytes
    • 描述: 包含在 PDF 中的标志文件或二进制图像数据的路径。您可以使用文件路径字符串或直接传递图像数据。
    • 示例:
      config.logo = "path/to/logo.jpg"  # Using a file path
      
    • 默认值: 无标志。
  2. 边距

    • 类型: Margins
    • 字段: toprightbottomleft(所有类型为 Number
    • 描述: 设置 PDF 文档的页面边距。
    • 示例:
      config.margins = Margins(top=5, right=5, bottom=5, left=5)
      
    • 默认值: 上、右、下和左均为 2 毫米。
  3. 收据位置

    • 类型: ReceiptPosition(枚举)
    • : TOPBOTTOMLEFT
    • 描述: 在 DANFE 中收据部分的定位。
    • 示例:
      config.receipt_pos = ReceiptPosition.BOTTOM
      
    • 默认值: 竖直方向为 TOP,横向为 LEFT
    • 注意: 在横向方向中,收据位置为最左侧;不允许自定义。
  4. 十进制配置

    • 类型: DecimalConfig
    • 字段: price_precisionquantity_precision(均为 int
    • 描述: 定义价格和数量的十进制位数。
    • 示例:
      config.decimal_config = DecimalConfig(price_precision=2, quantity_precision=3)
      
    • 默认: 4
  5. 税配置

    • 类型: TaxConfiguration(枚举)
    • : STANDARD_ICMS_IPIICMS_ST_ONLYWITHOUT_IPI
    • 描述: 指定要显示的税字段。
    • 示例:
      config.tax_configuration = TaxConfiguration.WITHOUT_IPI
      
    • 默认值: STANDARD_ICMS_IPI
    • 警告:此功能尚未实现。
  6. 发票显示

    • 类型: InvoiceDisplay(枚举)
    • : DUPLICATES_ONLYFULL_DETAILS
    • 描述: 控制 DANFE 中的发票部分的详细程度。
    • 示例::
      config.invoice_display = InvoiceDisplay.DUPLICATES_ONLY
      
    • 默认值: FULL_DETAILS
  7. 字体类型

    • 类型: FontType(枚举)
    • : COURIERTIMES
    • 描述: 在 PDF 文档中使用的字体样式。
    • 示例::
      config.font_type = FontType.COURIER
      
    • 默认值: TIMES
  8. 显示 PIS COFINS

    • 类型: Bool
    • : TrueFalse
    • 描述: 在 DANFE 总计中显示 PIS 和 COFINS 税。
    • 示例::
      config.display_pis_cofins = True
      
    • 默认值: False

带有自定义的使用示例

以下是如何设置带有完整自定义的 DanfeConfig 对象的方法:

from brazilfiscalreport.danfe import (
    Danfe,
    DanfeConfig,
    DecimalConfig,
    FontType,
    InvoiceDisplay,
    Margins,
    ReceiptPosition,
    TaxConfiguration,
)

# Path to the XML file
xml_file_path = 'nfe.xml'

# Load XML Content
with open(xml_file_path, "r", encoding="utf8") as file:
    xml_content = file.read()

# Create a configuration instance
config = DanfeConfig(
    logo='path/to/logo.png',
    margins=Margins(top=10, right=10, bottom=10, left=10),
    receipt_pos=ReceiptPosition.BOTTOM,
    decimal_config=DecimalConfig(price_precision=2, quantity_precision=2),
    tax_configuration=TaxConfiguration.ICMS_ST,
    invoice_display=InvoiceDisplay.FULL_DETAILS,
    font_type=FontType.TIMES
)

# Use this config when creating a Danfe instance
danfe = Danfe(xml_content, config=config)
danfe.output('output_danfe.pdf')

致谢 🙌

这是 nfe_utils 项目的分支,最初由 Edson Bernardino 创建。

维护者 🛠️

Engenere

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

brazilfiscalreport-0.4.2.tar.gz (29.7 kB 查看哈希值)

上传时间 源代码

构建分发

BrazilFiscalReport-0.4.2-py3-none-any.whl (31.3 kB 查看哈希值)

上传时间 Python 3

由以下支持