Skip to main content

一个用于以编程方式生成 Markdown 文档的 Python 库

Project description

Markdown Reporter

一个用于以编程方式生成 Markdown 文档的 Python 库。

功能特性

  • 支持 Markdown 标题(h1-h6),可选自动编号
  • 支持段落、超链接、图片、粗体、斜体、引用块
  • 支持有序列表、无序列表、任务列表、嵌套列表
  • 支持表格和 Pandas DataFrame 转换
  • 支持代码块(可去除缩进)
  • 支持 Mermaid 图表(饼图、XY 图表)
  • 支持上下文管理器
  • 文档内容直接输出到终端并可保存为文件

安装

uv pip install markdown-reporter

快速开始

from markdown_reporter import MarkdownReporter

with MarkdownReporter(use_section_number=True) as reporter:
    reporter.h1("项目报告")

    reporter.h2("简介")
    reporter.p("这是一个示例报告。")
    reporter.hyperlink("访问 Google", "https://google.com")

    reporter.h2("数据展示")
    reporter.table(
        headers=["名称", "分数"],
        rows=[["Alice", 95], ["Bob", 88]]
    )

    reporter.save("report.md")

API 参考

标题

reporter.h1("一级标题")
reporter.h2("二级标题")
reporter.h3("三级标题")
reporter.h4("四级标题")
reporter.h5("五级标题")
reporter.h6("六级标题")

启用章节编号后,二级到六级标题会自动添加编号(如 1.1, 1.2.1)。

文本

reporter.p("普通文本")
reporter.bold("粗体文本")  # 返回 **粗体文本**
reporter.italic("斜体文本")  # 返回 *斜体文本*
reporter.blockquote("引用文本")
reporter.horizontal_rule()  # 分隔线

链接与图片

reporter.hyperlink("链接文字", "https://example.com")
reporter.image("图片描述", "https://example.com/image.png")

列表

# 无序列表
reporter.ul(["苹果", "香蕉", "樱桃"])

# 带层级的无序列表
reporter.ul(["主项", "子项"], level=2)

# 有序列表
reporter.ol(["第一步", "第二步", "第三步"])

# 任务列表
reporter.task_list([
    ("已完成任务", True),
    ("未完成任务", False)
])

# 从字典生成嵌套列表
reporter.nested_list_from_dict({
    "水果": ["苹果", "香蕉"],
    "蔬菜": ["胡萝卜"]
})

表格

# 普通表格
reporter.table(
    headers=["姓名", "年龄"],
    rows=[["张三", 25], ["李四", 30]]
)

# 从 Pandas DataFrame 生成
reporter.table_from_pandas(df, index=False)

代码块

reporter.code_block("print('Hello')", lang="python", remove_indent=4)

Mermaid 图表

# 饼图
reporter.mermaid("pie", {
    "labels": [["已完成", 60], ["进行中", 30], ["未开始", 10]]
}, title="项目进度")

# 水平 XY 图表
reporter.mermaid("xy-horizontal", {
    "title": "月度销售额",
    "x_labels": ["1月", "2月", "3月"],
    "y_axis": "销售额(万元)",
    "values": [100, 150, 120]
})

# 垂直 XY 图表
reporter.mermaid("xy-vertical", {
    "title": "季度产量",
    "x_labels": ["Q1", "Q2", "Q3", "Q4"],
    "y_axis": "产量(件)",
    "values": [5000, 6000, 5500, 7000]
})

保存与重置

# 保存到文件
reporter.save("report.md")

# 重置内容
reporter.reset()

依赖

  • Python >= 3.12
  • pandas >= 2.0.0

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_reporter-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_reporter-1.0.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file markdown_reporter-1.0.0.tar.gz.

File metadata

  • Download URL: markdown_reporter-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for markdown_reporter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bdaf56c7edf59718b4afa89d0fe3b40501624a9bfa6fbfd0e5fda88dbf7a87cb
MD5 83cdf2a49386d480f03804ef83a2ff0e
BLAKE2b-256 8c476b0d7a498c7df0d863efc61472f5a310f70d97b772e9db0333b621e9340e

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_reporter-1.0.0.tar.gz:

Publisher: python-publish.yml on morning-start/markdown-reporter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file markdown_reporter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_reporter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4307f90255edd40be5a43b47b992c6522f649618e2369159fe62dff30e210337
MD5 940e1d3e72e7f6d4e812ee1b6d22733a
BLAKE2b-256 9885ff8485b6d2a0f46c1e21514d4a23308744a307072b979f6de387c3d1f5bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_reporter-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on morning-start/markdown-reporter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page