Chuyển đến nội dung
Skillpedia
PDF, Word và slide7.8k · #12970

reportlab-styles

Skill này làm gì

Skill này giải thích cách dùng các style đoạn văn có sẵn trong thư viện ReportLab cho PDF. Nó dành cho những người tạo tài liệu PDF bằng Python và gặp lỗi về style.

Khi nào nên dùng

  • ✓Khi một script ReportLab báo KeyError với tên style đoạn văn.
  • ✓Khi bạn muốn tiêu đề và nội dung được định dạng mà không phải tự tạo tên style mới.
  • ✓Khi bạn tạo báo cáo PDF và muốn giao diện chuẩn của các style mẫu.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reportlab-styles/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Script ReportLab của tôi bị lỗi KeyError ở style. Hãy sửa bằng các style có sẵn.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent lấy bảng style mẫu từ ReportLab và dùng các tên style có sẵn như Title, Heading1 và Normal. Nó tránh tạo style mới với tên đã có trong bảng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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
Câu hỏi thường gặp

Những style nào có sẵn?

Skill liệt kê Normal, Title, Heading1 đến Heading6, Bullet, Definition, Italic và Code.

Điều gì gây ra KeyError?

Skill nói lỗi do định nghĩa lại một tên style đã có, nên cách sửa là dùng tên có sẵn.

Tôi có cần style mới cho thiết kế của mình không?

Skill gợi ý dùng các style có sẵn trước và chỉ thêm style mới với tên duy nhất.

Chi tiết

Mô tả gốc của tác giả

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

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12970
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự