Office Document Handling
Use this skill for .docx, .xlsx, and .pptx files. Identify the file type first, check the required dependencies next, and then decide whether to create a new file or modify an existing one.
Follow These Rules First
- 1. Confirm the target format first: Word uses
python-docx, Excel uses openpyxl, and PowerPoint uses python-pptx. - Check whether the required dependency can be imported. If something is missing, tell the user exactly which package is required. Do not run
pip, pip3, python -m pip, sudo, or similar install commands automatically. - When editing an existing file, preserve the original by default and save the result as a new file. Only overwrite the original if the user explicitly asks for that.
- Do structural edits before visual fine-tuning. Avoid writing a large amount of low-value formatting code upfront.
- Only open the matching document in
references/ when an advanced API is needed. Do not load every reference file into context at once.
Dependency Tiers
Core Dependencies
- -
python-docx: for Word documents. - INLINECODE12 : for Excel workbooks.
- INLINECODE13 : for PowerPoint presentations.
Optional Dependencies
- -
pandas: only needed when converting between DataFrames and Excel. - INLINECODE15 : only needed when inserting images into Excel.
Check Dependencies
Use a read-only import check like this. If the check fails, stop implementation and report the missing package(s) directly.
CODEBLOCK0
Choose what to check based on the task:
- -
.docx tasks: check INLINECODE17 - INLINECODE18 tasks: check INLINECODE19
- INLINECODE20 tasks: check INLINECODE21
- DataFrame-related Excel tasks: also check INLINECODE22
- Excel image tasks: also check INLINECODE23
Handle Word Documents
Use python-docx for .docx files.
Create a New File
CODEBLOCK1
Modify an Existing File
CODEBLOCK2
Read These Resources First
- - Core API summary: INLINECODE26
- Create example: INLINECODE27
- Edit example: INLINECODE28
Handle Excel Workbooks
Use openpyxl for .xlsx files.
Create a New File
CODEBLOCK3
Modify an Existing File
CODEBLOCK4
Read These Resources First
- - Create example: INLINECODE31
- Charts: INLINECODE32
- Advanced features: INLINECODE33
- DataFrame integration: INLINECODE34
Handle PowerPoint Presentations
Use python-pptx for .pptx files.
Create a New File
CODEBLOCK5
Modify an Existing File
CODEBLOCK6
Read These Resources First
- - API quick reference: INLINECODE37
- Chart guide: INLINECODE38
- Full example: INLINECODE39
Resource Selection Rules
- - Basic Word file creation only: start with INLINECODE40
- Modify an existing Word file: start with INLINECODE41
- Basic Excel creation only: start with INLINECODE42
- Excel charts or conditional formatting: read
{baseDir}/references/xlsx_charts.md or {baseDir}/references/xlsx_features.md first - DataFrame, CSV to Excel, or worksheet analysis: confirm
pandas is available, then read INLINECODE46 - Basic PowerPoint generation only: start with INLINECODE47
- PowerPoint charts: read
{baseDir}/references/pptx_chart_guide.md first
Output Rules
- - Always save to a concrete target filename. Do not leave changes only in memory.
- If the user does not provide a filename, use a clear default such as
report.docx, budget.xlsx, or deck.pptx. - For edits to existing files, default to
updated_*.ext to avoid accidental overwrites.
Office文档处理
使用此技能处理 .docx、.xlsx 和 .pptx 文件。首先识别文件类型,然后检查所需的依赖项,最后决定是创建新文件还是修改现有文件。
首先遵循以下规则
- 1. 首先确认目标格式:Word 使用 python-docx,Excel 使用 openpyxl,PowerPoint 使用 python-pptx。
- 检查所需依赖项是否可以导入。如果缺少某些内容,请明确告知用户需要哪个包。不要自动运行 pip、pip3、python -m pip、sudo 或类似的安装命令。
- 编辑现有文件时,默认保留原始文件,并将结果保存为新文件。仅当用户明确要求时,才覆盖原始文件。
- 先进行结构性编辑,再进行视觉微调。避免预先编写大量低价值的格式代码。
- 仅在需要高级 API 时,才打开 references/ 中匹配的文档。不要一次性将所有参考文件加载到上下文中。
依赖项层级
核心依赖项
- - python-docx:用于 Word 文档。
- openpyxl:用于 Excel 工作簿。
- python-pptx:用于 PowerPoint 演示文稿。
可选依赖项
- - pandas:仅在 DataFrame 与 Excel 之间转换时需要。
- pillow:仅在向 Excel 插入图片时需要。
检查依赖项
使用如下只读导入检查。如果检查失败,停止实现并直接报告缺失的包。
python
import importlib
PACKAGE_MAP = {
docx: python-docx,
openpyxl: openpyxl,
pptx: python-pptx,
pandas: pandas,
PIL: pillow,
}
missing = []
for modulename, packagename in PACKAGE_MAP.items():
try:
importlib.importmodule(modulename)
except ModuleNotFoundError:
missing.append(package_name)
print(missing)
根据任务选择要检查的内容:
- - .docx 任务:检查 docx
- .xlsx 任务:检查 openpyxl
- .pptx 任务:检查 pptx
- 涉及 DataFrame 的 Excel 任务:同时检查 pandas
- Excel 图片任务:同时检查 PIL
处理 Word 文档
使用 python-docx 处理 .docx 文件。
创建新文件
python
from docx import Document
document = Document()
document.add_heading(文档标题, level=1)
document.add_paragraph(这是一个段落。)
document.save(new_doc.docx)
修改现有文件
python
from docx import Document
document = Document(existing_doc.docx)
document.add_paragraph(追加的内容)
document.save(updated_doc.docx)
首先阅读这些资源
- - 核心 API 摘要:{baseDir}/references/docxapisummary.md
- 创建示例:{baseDir}/scripts/createbasicdoc.py
- 编辑示例:{baseDir}/scripts/editexistingdoc.py
处理 Excel 工作簿
使用 openpyxl 处理 .xlsx 文件。
创建新文件
python
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = 数据
ws[A1] = 你好
ws.append([数据1, 数据2, 数据3])
wb.save(new_workbook.xlsx)
修改现有文件
python
from openpyxl import load_workbook
wb = loadworkbook(existingworkbook.xlsx)
ws = wb[Sheet1]
ws[A1] = 已更新的值
wb.save(updated_workbook.xlsx)
首先阅读这些资源
- - 创建示例:{baseDir}/scripts/createbasicworkbook.py
- 图表:{baseDir}/references/xlsxcharts.md
- 高级功能:{baseDir}/references/xlsxfeatures.md
- DataFrame 集成:{baseDir}/references/xlsx_pandas.md
处理 PowerPoint 演示文稿
使用 python-pptx 处理 .pptx 文件。
创建新文件
python
from pptx import Presentation
prs = Presentation()
slide = prs.slides.addslide(prs.slidelayouts[1])
slide.shapes.title.text = 幻灯片标题
slide.placeholders[1].text = 这是内容占位符。
prs.save(new_presentation.pptx)
修改现有文件
python
from pptx import Presentation
prs = Presentation(existing_presentation.pptx)
slide = prs.slides.addslide(prs.slidelayouts[1])
slide.shapes.title.text = 新幻灯片
slide.placeholders[1].text = 附加内容
prs.save(updated_presentation.pptx)
首先阅读这些资源
- - API 快速参考:{baseDir}/references/pptxapireference.md
- 图表指南:{baseDir}/references/pptxchartguide.md
- 完整示例:{baseDir}/scripts/create_presentation.py
资源选择规则
- - 仅创建基本 Word 文件:从 {baseDir}/scripts/createbasicdoc.py 开始
- 修改现有 Word 文件:从 {baseDir}/scripts/editexistingdoc.py 开始
- 仅创建基本 Excel 文件:从 {baseDir}/scripts/createbasicworkbook.py 开始
- Excel 图表或条件格式:先阅读 {baseDir}/references/xlsxcharts.md 或 {baseDir}/references/xlsxfeatures.md
- DataFrame、CSV 转 Excel 或工作表分析:确认 pandas 可用,然后阅读 {baseDir}/references/xlsxpandas.md
- 仅生成基本 PowerPoint:从 {baseDir}/scripts/createpresentation.py 开始
- PowerPoint 图表:先阅读 {baseDir}/references/pptxchartguide.md
输出规则
- - 始终保存到具体的目标文件名。不要仅将更改保留在内存中。
- 如果用户未提供文件名,请使用明确的默认名称,例如 report.docx、budget.xlsx 或 deck.pptx。
- 对于现有文件的编辑,默认使用 updated_*.ext 以避免意外覆盖。