document-processing について
document-processingは、Claude CodeやAIエージェント向けの文書処理スキルで、PDF、Word、PowerPoint、Excelを扱います。抽出、作成、編集、分析の作業に応じたツールと手順を案内します。
主な機能
- 01PDF、DOCX、PPTX、XLSXの処理
- 02テキストと表データの抽出
- 03オフィス文書の作成と編集
- 04PDFの結合、分割、OCR
- 05pandasによるExcelデータ分析
- 06OOXML編集手順の提示
活用シーン
- 01スキャンPDFからテキストを抽出
- 02Word文書やプレゼンテーションの作成・修正
- 03複数のExcelブックのデータ分析
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dirnbauer/ webconsulting-skills --skill document-processingインストール後、Claude Code で/document-processingを入力して読み込まれたか確認します。
詳細
ドキュメント処理
出典: このスキルは、**Anthropicのドキュメント処理スキル**をもとに改変したものです。
Claude CodeおよびAIエージェント向けのドキュメント処理スキル(pdf、docx、pptx、xlsx)です。
PDF、Word文書、PowerPointプレゼンテーション、Excelスプレッドシートなどのオフィス文書を作成、編集、分析できます。
クイックリファレンス:使用するツール
| タスク | ドキュメントの種類 | 最適なツール |
|---|---|---|
| テキストの抽出 | pdfplumber、pdftotext | |
| 結合/分割 | pypdf、qpdf | |
| フォームへの入力 | pdf-lib (JS)、pypdf | |
| 新規作成 | reportlab | |
| スキャン文書のOCR | pytesseract + pdf2image | |
| テキストの抽出 | DOCX | pandoc、markitdown |
| 新規作成 | DOCX | docx-js (JS) |
| 既存文書の編集 | DOCX | OOXML (展開/編集/再圧縮) |
| テキストの抽出 | PPTX | markitdown |
| 新規作成 | PPTX | html2pptx、PptxGenJS |
| 既存プレゼンテーションの編集 | PPTX | OOXML (展開/編集/再圧縮) |
| データ分析 | XLSX | pandas |
| 数式/書式設定 | XLSX | openpyxl |
PDFの処理
テキストの抽出
import pdfplumber
# Extract text with layout preservation
with pdfplumber.open("document.pdf") as pdf:
for page in pdf.pages:
text = page.extract_text()
print(text)
表の抽出
import pdfplumber
import pandas as pd
with pdfplumber.open("document.pdf") as pdf:
all_tables = []
for page in pdf.pages:
tables = page.extract_tables()
for table in tables:
if table:
df = pd.DataFrame(table[1:], columns=table[0])
all_tables.append(df)
# Combine all tables
if all_tables:
combined_df = pd.concat(all_tables, ignore_index=True)
combined_df.to_excel("extracted_tables.xlsx", index=False)
PDFの結合
from pypdf import PdfWriter, PdfReader
writer = PdfWriter()
for pdf_file in ["doc1.pdf", "doc2.pdf", "doc3.pdf"]:
reader = PdfReader(pdf_file)
for page in reader.pages:
writer.add_page(page)
with open("merged.pdf", "wb") as output:
writer.write(output)
PDFの分割
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
for i, page in enumerate(reader.pages):
writer = PdfWriter()
writer.add_page(page)
with open(f"page_{i+1}.pdf", "wb") as output:
writer.write(output)
ページの回転
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
writer = PdfWriter()
page = reader.pages[0]
page.rotate(90) # Rotate 90 degrees clockwise
writer.add_page(page)
with open("rotated.pdf", "wb") as output:
writer.write(output)
スキャンしたPDFのOCR
# Requires: pip install pytesseract pdf2image
import pytesseract
from pdf2image import convert_from_path
# Convert PDF to images
images = convert_from_path('scanned.pdf')
# OCR each page
text = ""
for i, image in enumerate(images):
text += f"Page {i+1}:\n"
text += pytesseract.image_to_string(image)
text += "\n\n"
print(text)
透かしの追加
from pypdf import PdfReader, PdfWriter
watermark = PdfReader("watermark.pdf").pages[0]
reader = PdfReader("document.pdf")
writer = PdfWriter()
for page in reader.pages:
page.merge_page(watermark)
writer.add_page(page)
with open("watermarked.pdf", "wb") as output:
writer.write(output)
パスワード保護
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
writer.encrypt("userpassword", "ownerpassword")
with open("encrypted.pdf", "wb") as output:
writer.write(output)
ReportLabでPDFを作成
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("report.pdf", pagesize=letter)
styles = getSampleStyleSheet()
story = []
# Add content
title = Paragraph("Report Title", styles['Title'])
story.append(title)
story.append(Spacer(1, 12))
body = Paragraph("This is the body of the report. " * 20, styles['Normal'])
story.append(body)
story.append(PageBreak())
# Page 2
story.append(Paragraph("Page 2", styles['Heading1']))
story.append(Paragraph("Content for page 2", styles['Normal']))
doc.build(story)
コマンドラインツール
# Extract text (poppler-utils)
pdftotext input.pdf output.txt
pdftotext -layout input.pdf output.txt # Preserve layout
# Merge PDFs (qpdf)
qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf
# Split pages
qpdf input.pdf --pages . 1-5 -- pages1-5.pdf
# Rotate pages
qpdf input.pdf output.pdf --rotate=+90:1
# Remove password
qpdf --password=mypassword --decrypt encrypted.pdf decrypted.pdf
# Extract images
pdfimages -j input.pdf output_prefix
Word文書(DOCX)の処理
テキストの抽出
# Convert to markdown with pandoc
pandoc document.docx -o output.md
# With tracked changes preserved
pandoc --track-changes=all document.docx -o output.md
新しい文書の作成 (docx-js)
import { Document, Paragraph, TextRun, HeadingLevel, Packer } from 'docx';
import * as fs from 'fs';
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
text: "Document Title",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph({
children: [
new TextRun("This is a "),
new TextRun({
text: "bold",
bold: true,
}),
new TextRun(" word in a paragraph."),
],
}),
new Paragraph({
text: "This is another paragraph.",
}),
],
}],
});
// Export to file
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("output.docx", buffer);
表を含む文書の作成
import { Document, Paragraph, Table, TableRow, TableCell, Packer } from 'docx';
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Header 1")] }),
new TableCell({ children: [new Paragraph("Header 2")] }),
new TableCell({ children: [new Paragraph("Header 3")] }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Cell 1")] }),
new TableCell({ children: [new Paragraph("Cell 2")] }),
new TableCell({ children: [new Paragraph("Cell 3")] }),
],
}),
],
});
const doc = new Document({
sections: [{
children: [
new Paragraph({ text: "Table Example", heading: HeadingLevel.HEADING_1 }),
table,
],
}],
});
既存文書の編集 (OOXML)
複雑な編集では、OOXMLを直接操作します。
-
文書を展開します:
python ooxml/scripts/unpack.py document.docx unpacked/ -
XMLファイルを編集します(主に
word/document.xml) -
検証して再圧縮します:
python ooxml/scripts/validate.py unpacked/ --original document.docx python ooxml/scripts/pack.py unpacked/ output.docx
変更履歴を記録するワークフロー
変更履歴を記録しながら文書をレビューする場合:
# 1. Get current state
pandoc --track-changes=all document.docx -o current.md
# 2. Unpack
python ooxml/scripts/unpack.py document.docx unpacked/
# 3. Edit using tracked change patterns
# Use <w:ins> for insertions, <w:del> for deletions
# 4. Pack final document
python ooxml/scripts/pack.py unpacked/ reviewed.docx
PowerPoint(PPTX)の処理
テキストの抽出
python -m markitdown presentation.pptx
新しいプレゼンテーションの作成 (PptxGenJS)
import PptxGenJS from 'pptxgenjs';
const pptx = new PptxGenJS();
// Slide 1 - Title
const slide1 = pptx.addSlide();
slide1.addText("Presentation Title", {
x: 1, y: 2, w: 8, h: 1.5,
fontSize: 36,
bold: true,
color: "363636",
align: "center",
});
slide1.addText("Subtitle goes here", {
x: 1, y: 3.5, w: 8, h: 0.5,
fontSize: 18,
color: "666666",
align: "center",
});
// Slide 2 - Content
const slide2 = pptx.addSlide();
slide2.addText("Key Points", {
x: 0.5, y: 0.5, w: 9, h: 0.8,
fontSize: 28,
bold: true,
});
slide2.addText([
{ text: "• First important point\n", options: { bullet: true } },
{ text: "• Second important point\n", options: { bullet: true } },
{ text: "• Third important point\n", options: { bullet: true } },
], {
x: 0.5, y: 1.5, w: 9, h: 3,
fontSize: 18,
});
// Slide 3 - Chart
const slide3 = pptx.addSlide();
slide3.addChart(pptx.ChartType.bar, [
{ name: "Q1", labels: ["Jan", "Feb", "Mar"], values: [100, 200, 300] },
{ name: "Q2", labels: ["Apr", "May", "Jun"], values: [150, 250, 350] },
], {
x: 1, y: 1, w: 8, h: 4,
showLegend: true,
legendPos: "b",
});
// Save
pptx.writeFile("output.pptx");
既存プレゼンテーションの編集 (OOXML)
# 1. Unpack
python ooxml/scripts/unpack.py presentation.pptx unpacked/
# 2. Key files:
# - ppt/slides/slide1.xml, slide2.xml, etc.
# - ppt/notesSlides/ for speaker notes
# - ppt/theme/ for styling
# 3. Validate and pack
python ooxml/scripts/validate.py unpacked/ --original presentation.pptx
python ooxml/scripts/pack.py unpacked/ output.pptx
サムネイル一覧の作成
# Create visual overview of all slides
python scripts/thumbnail.py presentation.pptx --cols 4
スライドを画像に変換
# Convert to PDF first
soffice --headless --convert-to pdf presentation.pptx
# Then PDF to images
pdftoppm -jpeg -r 150 presentation.pdf slide
# Creates slide-1.jpg, slide-2.jpg, etc.
Excel(XLSX)の処理
Pandasによるデータ分析
import pandas as pd
# Read Excel
df = pd.read_excel('file.xlsx') # Default: first sheet
all_sheets = pd.read_excel('file.xlsx', sheet_name=None) # All sheets as dict
# Analyze
df.head() # Preview data
df.info() # Column info
df.describe() # Statistics
# Filter and transform
filtered = df[df['Sales'] > 1000]
grouped = df.groupby('Category')['Revenue'].sum()
# Write Excel
df.to_excel('output.xlsx', index=False)
数式を含むExcelの作成 (openpyxl)
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment
wb = Workbook()
sheet = wb.active
# Add data
sheet['A1'] = 'Product'
sheet['B1'] = 'Price'
sheet['C1'] = 'Quantity'
sheet['D1'] = 'Total'
# Header formatting
for cell in ['A1', 'B1', 'C1', 'D1']:
sheet[cell].font = Font(bold=True, color='FFFFFF')
sheet[cell].fill = PatternFill('solid', start_color='4472C4')
sheet[cell].alignment = Alignment(horizontal='center')
# Add data rows
data = [
('Widget A', 10.00, 5),
('Widget B', 15.00, 3),
('Widget C', 20.00, 8),
]
for row_idx, (product, price, qty) in enumerate(data, start=2):
sheet[f'A{row_idx}'] = product
sheet[f'B{row_idx}'] = price
sheet[f'C{row_idx}'] = qty
# FORMULA - not hardcoded value!
sheet[f'D{row_idx}'] = f'=B{row_idx}*C{row_idx}'
# Add sum formula at bottom
last_row = len(data) + 2
sheet[f'D{last_row}'] = f'=SUM(D2:D{last_row-1})'
# Column width
sheet.column_dimensions['A'].width = 15
sheet.column_dimensions['B'].width = 10
sheet.column_dimensions['C'].width = 10
sheet.column_dimensions['D'].width = 10
wb.save('output.xlsx')
財務モデルの標準
色分け
from openpyxl.styles import Font
# Industry-standard colors
BLUE = Font(color='0000FF') # Hardcoded inputs
BLACK = Font(color='000000') # Formulas
GREEN = Font(color='008000') # Links from other sheets
RED = Font(color='FF0000') # External links
# Apply to cells
sheet['B5'].font = BLUE # User input
sheet['B6'].font = BLACK # Formula
数値の書式設定
# Currency with thousands separator
sheet['B5'].number_format = '$#,##0'
# Percentage with one decimal
sheet['B6'].number_format = '0.0%'
# Zeros as dashes
sheet['B7'].number_format = '$#,##0;($#,##0);"-"'
# Multiples
sheet['B8'].number_format = '0.0x'
重要:ハードコードされた値ではなく数式を使用する
# ❌ WRONG - Hardcoding calculated values
total = df['Sales'].sum()
sheet['B10'] = total # Hardcodes 5000
# ✅ CORRECT - Use Excel formulas
sheet['B10'] = '=SUM(B2:B9)'
# ❌ WRONG - Computing in Python
growth = (current - previous) / previous
sheet['C5'] = growth
# ✅ CORRECT - Excel formula
sheet['C5'] = '=(C4-C2)/C2'
既存のExcelファイルの編集
from openpyxl import load_workbook
# Load with formulas preserved
wb = load_workbook('existing.xlsx')
sheet = wb.active
# Modify cells
sheet['A1'] = 'New Value'
sheet.insert_rows(2)
sheet.delete_cols(3)
# Add new sheet
new_sheet = wb.create_sheet('Analysis')
new_sheet['A1'] = 'Data'
wb.save('modified.xlsx')
数式の再計算
数式を含むExcelファイルを作成または変更した後:
# Recalculate all formulas using LibreOffice
python recalc.py output.xlsx
依存関係
必要に応じてインストールします。
# PDF
pip install pypdf pdfplumber reportlab pytesseract pdf2image
# DOCX
npm install -g docx
pip install "markitdown[docx]"
# PPTX
npm install -g pptxgenjs
pip install "markitdown[pptx]"
# XLSX
pip install pandas openpyxl
# Command line tools
sudo apt-get install poppler-utils qpdf libreoffice pandoc
タスクのクイックリファレンス
| 実行したいこと | コマンド/コード |
|---|---|
| PDFテキストの抽出 | pdfplumber.open(f).pages[0].extract_text() |
| PDFの結合 | pypdf.PdfWriter() + ループ |
| PDFの分割 | ページごとに PdfWriter() を1つ使用 |
| スキャンしたPDFのOCR | pdf2image → pytesseract |
| DOCXをMDに変換 | pandoc doc.docx -o doc.md |
| DOCXの作成 | docx-js (JavaScript) |
| PPTXテキストの抽出 | python -m markitdown pres.pptx |
| PPTXの作成 | PptxGenJS (JavaScript) |
| Excelの分析 | pandas.read_excel() |
| 数式を含むExcel | openpyxl |
クレジットと帰属
このスキルは、**Anthropic**による優れた成果をもとにしています。
元のリポジトリ:https://github.com/anthropics/skills/tree/main/skills/document-processing
このスキル集の形成に役立った、Anthropicの寛大なオープンソースへの貢献に心より感謝します。Anthropic
このスキル集向けにwebconsulting.atが改変しました
コメント
Markdown 対応