跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12970

reportlab-styles

它能做什么

这项技能说明如何在 ReportLab 库中使用现成的段落样式制作 PDF。它适合用 Python 制作 PDF 文档并遇到样式错误的人。

适用场景

  • ✓当 ReportLab 脚本因段落样式名称而抛出 KeyError 的时候。
  • ✓当你希望标题和正文使用样式,而不必手动创建新样式名称的时候。
  • ✓当你制作 PDF 报告,并希望采用示例样式的标准外观的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reportlab-styles/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reportlab-styles/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 ReportLab 脚本因样式出现 KeyError 而崩溃。请使用内置样式修复它。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会从 ReportLab 获取示例样式表,并使用现有的样式名称,例如 Title、Heading1 和 Normal。它会避免创建与样式表中已有名称相同的新样式。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

ReportLab Pre-Defined Styles Reference

Overview

ReportLab's reportlab.lib.styles.getSampleStyleSheet() provides a collection of pre-defined paragraph styles. Use these existing styles rather than redefining them to avoid KeyError exceptions.

Available Pre-Defined Styles

The standard stylesheet includes these commonly-used styles:

Style Name Purpose
Normal Default body text
Title Document title (large, bold)
Heading1 Level 1 section heading
Heading2 Level 2 section heading
Heading3 Level 3 section heading
Heading4 Level 4 section heading
Heading5 Level 5 section heading
Heading6 Level 6 section heading
Bullet Bulleted list items
Definition Definition list terms
Italic Italicized text
Code Monospace code text

Correct Usage Pattern

from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph

# Get the stylesheet
styles = getSampleStyleSheet()

# Use existing styles directly (CORRECT)
title = Paragraph("My Document Title", styles['Title'])
heading = Paragraph("Section Header", styles['Heading1'])
body = Paragraph("Regular text content", styles['Normal'])

Common Mistake to Avoid

# WRONG - Don't redefine existing style names
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

styles = getSampleStyleSheet()

# This will cause KeyError when you try to use 'Title' later
styles.add(ParagraphStyle(
    name='Title',  # Overwrites the pre-defined 'Title' style
    fontSize=24,
    # ... incomplete definition
))

# Later code expecting the full 'Title' style will fail
paragraph = Paragraph("Title Text", styles['Title'])  # KeyError!

Best Practices

  1. Use existing styles as-is when they meet your needs
  2. Create new style names for custom styles (e.g., MyCustomTitle, CustomHeading)
  3. Base custom styles on existing ones when you need modifications:
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

styles = getSampleStyleSheet()

# Create a NEW style with a unique name
custom_title = ParagraphStyle(
    name='CustomTitle',  # Unique name, doesn't conflict
    parent=styles['Title'],  # Base it on existing Title
    fontSize=28,
    spaceAfter=30
)
styles.add(custom_title)

# Now both styles are available
styles['Title']       # Original pre-defined style
styles['CustomTitle'] # Your custom variant

Quick Reference Checklist

  • Import getSampleStyleSheet() from reportlab.lib.styles
  • Call styles = getSampleStyleSheet() once at the start
  • Access styles via styles['StyleName'] (case-sensitive)
  • Do NOT add styles with names that already exist in the stylesheet
  • Use unique names for any custom styles you create
  • Common available styles: Normal, Title, Heading1-6, Bullet, Code, Italic
常见问题

有哪些内置样式?

这项技能列出了 Normal、Title、Heading1 到 Heading6、Bullet、Definition、Italic 和 Code。

KeyError 是什么原因造成的?

这项技能指出,该错误来自重新定义了已有的样式名称,因此解决办法是使用内置名称。

我的设计需要新样式吗?

这项技能建议先使用现有样式,只有在必要时才以唯一的名称添加新样式。

详情

作者的原始描述

Use reportlab's pre-defined styles from getSampleStyleSheet() correctly

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12970
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能