docx
このスキルは、ユーザーがWord文書(.docxファイル)を作成、読み込み、編集、操作したいときに使用します。以下の場合に実行してください:「Word文書」「.docx」などの記述、または目次・見出し・ページ番号・レターヘッドなどのフォーマットを含む専門的な文書の作成リクエスト。また、.docxファイルのコンテンツ抽出・再編成、文書への画像挿入・置換、Word形式での検索置換、変更履歴やコメント機能の使用、コンテンツを整形したWord文書への変換の場合も対象です。ユーザーが「レポート」「メモ」「手紙」「テンプレート」などの成果物をWord形式または.docxファイルで求める場合はこのスキルを使用してください。PDF、スプレッドシート、Google Docs、文書作成と無関係なコーディングタスクには使用しないでください。
description の原文を見る
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation.
SKILL.md 本文
DOCX作成、編集、分析
概要
.docxファイルはXMLファイルを含むZIPアーカイブです。
クイックリファレンス
| タスク | アプローチ |
|---|---|
| コンテンツの読み込み/分析 | pandoc または生XMLのために展開 |
| 新規ドキュメント作成 | docx-js を使用 - 下記「新規ドキュメント作成」参照 |
| 既存ドキュメント編集 | 展開 → XMLを編集 → 再圧縮 - 下記「既存ドキュメント編集」参照 |
.docから.docxへの変換
レガシーの.docファイルは編集前に変換する必要があります:
python scripts/office/soffice.py --headless --convert-to docx document.doc
コンテンツの読み込み
# 変更追跡を含むテキスト抽出
pandoc --track-changes=all document.docx -o output.md
# 生XMLアクセス
python scripts/office/unpack.py document.docx unpacked/
画像への変換
python scripts/office/soffice.py --headless --convert-to pdf document.docx
pdftoppm -jpeg -r 150 document.pdf page
変更追跡の確定
すべての変更追跡が確定されたクリーンなドキュメントを生成します(LibreOfficeが必要):
python scripts/accept_changes.py input.docx output.docx
新規ドキュメント作成
JavaScriptで.docxファイルを生成してから検証します。インストール: npm install -g docx
セットアップ
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ImageRun,
Header, Footer, AlignmentType, PageOrientation, LevelFormat, ExternalHyperlink,
InternalHyperlink, Bookmark, FootnoteReferenceRun, PositionalTab,
PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader,
TabStopType, TabStopPosition, Column, SectionType,
TableOfContents, HeadingLevel, BorderStyle, WidthType, ShadingType,
VerticalAlign, PageNumber, PageBreak } = require('docx');
const doc = new Document({ sections: [{ children: [/* content */] }] });
Packer.toBuffer(doc).then(buffer => fs.writeFileSync("doc.docx", buffer));
検証
ファイルを作成した後、検証します。検証に失敗した場合は、展開してXMLを修正し、再圧縮します。
python scripts/office/validate.py doc.docx
ページサイズ
// 重要: docx-jsはA4がデフォルトで、US Letterではありません
// 一貫した結果を得るため、常にページサイズを明示的に設定してください
sections: [{
properties: {
page: {
size: {
width: 12240, // 8.5インチ(DXA単位)
height: 15840 // 11インチ(DXA単位)
},
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1インチマージン
}
},
children: [/* content */]
}]
一般的なページサイズ(DXA単位、1440 DXA = 1インチ):
| 用紙 | 幅 | 高さ | コンテンツ幅(1インチマージン) |
|---|---|---|---|
| US Letter | 12,240 | 15,840 | 9,360 |
| A4(デフォルト) | 11,906 | 16,838 | 9,026 |
横向きの向き: docx-jsは内部的に幅と高さを入れ替えるため、縦向きの寸法を渡してそれに処理させます:
size: {
width: 12240, // 短いエッジを幅として設定
height: 15840, // 長いエッジを高さとして設定
orientation: PageOrientation.LANDSCAPE // docx-jsがXMLで交換します
},
// コンテンツ幅 = 15840 - 左マージン - 右マージン(長いエッジを使用)
スタイル(組み込みヘッディングをオーバーライド)
デフォルトフォントとしてArialを使用します(ユニバーサルに対応)。可読性のためにタイトルを黒で保ちます。
const doc = new Document({
styles: {
default: { document: { run: { font: "Arial", size: 24 } } }, // 12ptデフォルト
paragraphStyles: [
// 重要: 組み込みスタイルをオーバーライドするには正確なIDを使用してください
{ id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 32, bold: true, font: "Arial" },
paragraph: { spacing: { before: 240, after: 240 }, outlineLevel: 0 } }, // outlineLevel は TOC に必須
{ id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 28, bold: true, font: "Arial" },
paragraph: { spacing: { before: 180, after: 180 }, outlineLevel: 1 } },
]
},
sections: [{
children: [
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun("Title")] }),
]
}]
});
リスト(Unicode の箇条書き記号は使用しないでください)
// ❌ 誤り - 箇条書き文字を手動で挿入しないでください
new Paragraph({ children: [new TextRun("• Item")] }) // 不可
new Paragraph({ children: [new TextRun("\u2022 Item")] }) // 不可
// ✅ 正解 - LevelFormat.BULLET で番号付け設定を使用してください
const doc = new Document({
numbering: {
config: [
{ reference: "bullets",
levels: [{ level: 0, format: LevelFormat.BULLET, text: "•", alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] },
{ reference: "numbers",
levels: [{ level: 0, format: LevelFormat.DECIMAL, text: "%1.", alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] },
]
},
sections: [{
children: [
new Paragraph({ numbering: { reference: "bullets", level: 0 },
children: [new TextRun("Bullet item")] }),
new Paragraph({ numbering: { reference: "numbers", level: 0 },
children: [new TextRun("Numbered item")] }),
]
}]
});
// ⚠️ 各参照は独立した番号付けを作成します
// 同じ参照 = 継続(1,2,3 その後 4,5,6)
// 異なる参照 = リセット(1,2,3 その後 1,2,3)
テーブル
重要: テーブルは二重幅が必要です - テーブルの columnWidths と各セルの width の両方を設定してください。両方がないと、テーブルはいくつかのプラットフォームで誤ってレンダリングされます。
// 重要: 一貫したレンダリング用にテーブル幅を常に設定してください
// 重要: ShadingType.CLEAR(SOLID ではなく)を使用して黒い背景を防いでください
const border = { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" };
const borders = { top: border, bottom: border, left: border, right: border };
new Table({
width: { size: 9360, type: WidthType.DXA }, // 常に DXA を使用してください
columnWidths: [4680, 4680], // テーブル幅に合計する必要があります(DXA: 1440 = 1インチ)
rows: [
new TableRow({
children: [
new TableCell({
borders,
width: { size: 4680, type: WidthType.DXA }, // 各セルにも設定してください
shading: { fill: "D5E8F0", type: ShadingType.CLEAR }, // SOLID ではなく CLEAR
margins: { top: 80, bottom: 80, left: 120, right: 120 }, // セルパディング(内部、幅に追加されません)
children: [new Paragraph({ children: [new TextRun("Cell")] })]
})
]
})
]
})
テーブル幅の計算:
常に WidthType.DXA を使用してください — WidthType.PERCENTAGE はGoogle Docsで機能しません。
// テーブル幅 = columnWidths の合計 = コンテンツ幅
// US Letter(1インチマージン): 12240 - 2880 = 9360 DXA
width: { size: 9360, type: WidthType.DXA },
columnWidths: [7000, 2360] // テーブル幅に合計する必要があります
幅のルール:
- 常に
WidthType.DXAを使用してください —WidthType.PERCENTAGEは使用しないでください(Google Docsと互換性がありません) - テーブル幅は
columnWidthsの合計と等しくなければなりません - セル
widthは対応するcolumnWidthと一致する必要があります - セル
marginsは内部パディングです — コンテンツ領域を減らし、セルの幅に追加しません - フルwidth テーブルの場合: コンテンツ幅を使用します(ページ幅から左右のマージンを引いたもの)
画像
// 重要: type パラメータは必須です
new Paragraph({
children: [new ImageRun({
type: "png", // 必須: png、jpg、jpeg、gif、bmp、svg
data: fs.readFileSync("image.png"),
transformation: { width: 200, height: 150 },
altText: { title: "Title", description: "Desc", name: "Name" } // 3つすべて必須
})]
})
ページ区切り
// 重要: PageBreak は Paragraph 内に含める必要があります
new Paragraph({ children: [new PageBreak()] })
// または pageBreakBefore を使用してください
new Paragraph({ pageBreakBefore: true, children: [new TextRun("New page")] })
ハイパーリンク
// 外部リンク
new Paragraph({
children: [new ExternalHyperlink({
children: [new TextRun({ text: "Click here", style: "Hyperlink" })],
link: "<external-url>",
})]
})
// 内部リンク(ブックマーク + 参照)
// 1. 目的地にブックマークを作成
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [
new Bookmark({ id: "chapter1", children: [new TextRun("Chapter 1")] }),
]})
// 2. リンク
new Paragraph({ children: [new InternalHyperlink({
children: [new TextRun({ text: "See Chapter 1", style: "Hyperlink" })],
anchor: "chapter1",
})]})
脚注
const doc = new Document({
footnotes: {
1: { children: [new Paragraph("Source: Annual Report 2024")] },
2: { children: [new Paragraph("See appendix for methodology")] },
},
sections: [{
children: [new Paragraph({
children: [
new TextRun("Revenue grew 15%"),
new FootnoteReferenceRun(1),
new TextRun(" using adjusted metrics"),
new FootnoteReferenceRun(2),
],
})]
}]
});
タブストップ
// 右寄せテキストを同じ行に(例えば、タイトルの反対側に日付)
new Paragraph({
children: [
new TextRun("Company Name"),
new TextRun("\tJanuary 2025"),
],
tabStops: [{ type: TabStopType.RIGHT, position: TabStopPosition.MAX }],
})
// ドットリーダー(例えば、目次スタイル)
new Paragraph({
children: [
new TextRun("Introduction"),
new TextRun({ children: [
new PositionalTab({
alignment: PositionalTabAlignment.RIGHT,
relativeTo: PositionalTabRelativeTo.MARGIN,
leader: PositionalTabLeader.DOT,
}),
"3",
]}),
],
})
多段レイアウト
// 同じ幅の列
sections: [{
properties: {
column: {
count: 2, // 列の数
space: 720, // 列間のギャップ(DXA単位、720 = 0.5インチ)
equalWidth: true,
separate: true, // 列間の縦線
},
},
children: [/* コンテンツは列全体に自然に流れます */]
}]
// カスタム幅の列(equalWidth は false である必要があります)
sections: [{
properties: {
column: {
equalWidth: false,
children: [
new Column({ width: 5400, space: 720 }),
new Column({ width: 3240 }),
],
},
},
children: [/* content */]
}]
type: SectionType.NEXT_COLUMN を使用した新しいセクションで列区切りを強制します。
目次
// 重要: ヘッディングは HeadingLevel のみを使用する必要があります - カスタムスタイルはありません
new TableOfContents("Table of Contents", { hyperlink: true, headingStyleRange: "1-3" })
ヘッダー/フッター
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } } // 1440 = 1インチ
},
headers: {
default: new Header({ children: [new Paragraph({ children: [new TextRun("Header")] })] })
},
footers: {
default: new Footer({ children: [new Paragraph({
children: [new TextRun("Page "), new TextRun({ children: [PageNumber.CURRENT] })]
})] })
},
children: [/* content */]
}]
docx-js の重要なルール
- ページサイズを明示的に設定してください - docx-js はA4がデフォルトです。米国ドキュメントにはUS Letterを使用してください(12240 x 15840 DXA)
- 横向き: 縦向きの寸法を設定してください - docx-js は内部的に幅と高さを入れ替えます。短いエッジを
widthとして、長いエッジをheightとして設定し、orientation: PageOrientation.LANDSCAPEを設定してください \nは使用しないでください - 別の Paragraph 要素を使用してください- unicode の箇条書き記号は使用しないでください -
LevelFormat.BULLETを番号付け設定で使用してください - PageBreak は Paragraph 内である必要があります - スタンドアロンは無効なXMLを作成します
- ImageRun は
typeが必須です - 常に png/jpg 等を指定してください - 常にテーブル
widthを DXA で設定してください -WidthType.PERCENTAGEは使用しないでください(Google Docsで機能しません) - テーブルは二重幅が必要です -
columnWidths配列とセルwidthの両方、両方が一致する必要があります - テーブル幅 = columnWidths の合計 - DXA の場合、正確に合計することを確認してください
- 常にセルマージンを追加してください - 可読性のあるパディングに
margins: { top: 80, bottom: 80, left: 120, right: 120 }を使用してください ShadingType.CLEARを使用してください - テーブルの網掛けに SOLID は使用しないでください- テーブルを区切り線/ルールとして使用しないでください - セルは最小の高さを持ち、空のボックスとしてレンダリングされます(ヘッダー/フッターを含む)。代わりに Paragraph で
border: { bottom: { style: BorderStyle.SINGLE, size: 6, color: "2E75B6", space: 1 } }を使用してください。2列のフッターの場合、テーブルではなくタブストップを使用してください(タブストップセクション参照) - TOC は HeadingLevel のみが必要です - ヘッディング段落にカスタムスタイルはありません
- 組み込みスタイルをオーバーライドしてください - 正確なIDを使用してください: "Heading1"、"Heading2" など
outlineLevelを含めてください - TOC に必須です(H1 の場合は 0、H2 の場合は 1 など)
既存ドキュメント編集
順序どおりに3つのステップすべてに従ってください。
ステップ 1: 展開
python scripts/office/unpack.py document.docx unpacked/
XMLを抽出し、きれいにプリントして、隣接するランを統合し、スマートクォートをXMLエンティティ(“ など)に変換して、編集時に保持されるようにします。--merge-runs false を使用してラン統合をスキップします。
ステップ 2: XML を編集
unpacked/word/ 内のファイルを編集します。パターンについては下記「XML リファレンス」を参照してください。
**変更追跡とコメントに「AI Assistant」を作成者として使用してください。**ユーザーが明示的に別の名前の使用をリクエストした場合を除きます。
**文字列置換には Edit ツールを直接使用してください。Python スクリプトを書かないでください。**スクリプトは不要な複雑さをもたらします。Edit ツールは正確に何が置換されているかを示します。
**重要: 新しいコンテンツにはスマートクォートを使用してください。**アポストロフィまたはクォートを含むテキストを追加する場合は、XMLエンティティを使用してスマートクォートを生成してください:
<!-- プロフェッショナルなタイポグラフィにはこれらのエンティティを使用します -->
<w:t>Here’s a quote: “Hello”</w:t>
| エンティティ | 文字 |
|---|---|
‘ | ' (左シングル) |
’ | ' (右シングル/アポストロフィ) |
“ | " (左ダブル) |
” | " (右ダブル) |
コメント追加: comment.py を使用して複数のXMLファイル全体でボイラープレートを処理します(テキストは事前にエスケープされたXML である必要があります):
python scripts/comment.py unpacked/ 0 "Comment text with & and ’"
python scripts/comment.py unpacked/ 1 "Reply text" --parent 0 # コメント 0 への返信
python scripts/comment.py unpacked/ 0 "Text" --author "Custom Author" # カスタム作成者名
その後、document.xml にマーカーを追加します(XML リファレンス内の「コメント」を参照)。
ステップ 3: 圧縮
python scripts/office/pack.py unpacked/ output.docx --original document.docx
自動修復で検証し、XMLを圧縮して、DOCX を作成します。検証をスキップするには --validate false を使用してください。
自動修復は以下を修正します:
durableId>= 0x7FFFFFFF(有効なIDを再生成)<w:t>にxml:space="preserve"が不足している(空白を使用)
自動修復は修正しません:
- 形式が正しくないXML、無効な要素のネスト、不足している関係、スキーマ違反
一般的な落とし穴
<w:r>要素全体を置換してください: 変更追跡を追加する場合、<w:r>...</w:r>ブロック全体を<w:del>...<w:ins>...兄弟として置換してください。追跡変更タグをラン内に挿入しないでください。<w:rPr>形式を保持してください: 元のランの<w:rPr>ブロックを追跡変更ランにコピーして、太字、フォントサイズなどを保持してください。
XML リファレンス
スキーマのコンプライアンス
<w:pPr>内の要素の順序:<w:pStyle>、<w:numPr>、<w:spacing>、<w:ind>、<w:jc>、<w:rPr>は最後- 空白: 先頭/末尾のスペースを持つ
<w:t>にxml:space="preserve"を追加してください - RSID: 8桁の16進数である必要があります(例:
00AB1234)
変更追跡
挿入:
<w:ins w:id="1" w:author="AI Assistant" w:date="2025-01-01T00:00:00Z">
<w:r><w:t>inserted text</w:t></w:r>
</w:ins>
削除:
<w:del w:id="2" w:author="AI Assistant" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>deleted text</w:delText></w:r>
</w:del>
<w:del> 内: <w:t> の代わりに <w:delText> を使用し、<w:instrText> の代わりに <w:delInstrText> を使用してください。
最小限の編集 - 変更されたものだけをマークしてください:
<!-- 「30 days」を「60 days」に変更 -->
<w:r><w:t>The term is </w:t></w:r>
<w:del w:id="1" w:author="AI Assistant" w:date="...">
<w:r><w:delText>30</w:delText></w:r>
</w:del>
<w:ins w:id="2" w:author="AI Assistant" w:date="...">
<w:r><w:t>60</w:t></w:r>
</w:ins>
<w:r><w:t> days.</w:t></w:r>
段落/リストアイテム全体の削除 - 段落から ALL コンテンツを削除する場合、次の段落とマージするように段落記号も削除としてマークしてください。<w:pPr><w:rPr> 内に <w:del/> を追加してください:
<w:p>
<w:pPr>
<w:numPr>...</w:numPr> <!-- 存在する場合はリスト番号付け -->
<w:rPr>
<w:del w:id="1" w:author="AI Assistant" w:date="2025-01-01T00:00:00Z"/>
</w:rPr>
</w:pPr>
<w:del w:id="2" w:author="AI Assistant" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>削除される段落全体のコンテンツ...</w:delText></w:r>
</w:del>
</w:p>
<w:pPr><w:rPr> に <w:del/> がない場合、変更を確定することで空の段落/リストアイテムが残ります。
別の作成者の挿入を却下する - 削除をその挿入内にネストさせます:
<w:ins w:author="Jane" w:id="5">
<w:del w:author="AI Assistant" w:id="10">
<w:r><w:delText>their inserted text</w:delText></w:r>
</w:del>
</w:ins>
別の作成者の削除を復元する - 後に挿入を追加します(その削除を変更しないでください):
<w:del w:author="Jane" w:id="5">
<w:r><w:delText>deleted text</w:delText></w:r>
</w:del>
<w:ins w:author="AI Assistant" w:id="10">
<w:r><w:t>deleted text</w:t></w:r>
</w:ins>
コメント
comment.py を実行した後(ステップ 2 参照)、document.xml にマーカーを追加します。返信の場合は、--parent フラグを使用して親内にネストされたマーカーを使用します。
重要: <w:commentRangeStart> と <w:commentRangeEnd> は <w:r> の兄弟であり、<w:r> の内部ではありません。
<!-- コメントマーカーは w:p の直接の子、w:r の内部ではありません -->
<w:commentRangeStart w:id="0"/>
<w:del w:id="1" w:author="AI Assistant" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>deleted</w:delText></w:r>
</w:del>
<w:r><w:t> more text</w:t></w:r>
<w:commentRangeEnd w:id="0"/>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="0"/></w:r>
<!-- 返信 1 がネストされたコメント 0 -->
<w:commentRangeStart w:id="0"/>
<w:commentRangeStart w:id="1"/>
<w:r><w:t>text</w:t></w:r>
<w:commentRangeEnd w:id="1"/>
<w:commentRangeEnd w:id="0"/>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="0"/></w:r>
<w:r><w:rPr><w:rStyle w:
ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- LuizEduPP
- リポジトリ
- LuizEduPP/skills
- ライセンス
- MIT
- 最終更新
- 2026/5/9
Source: https://github.com/LuizEduPP/skills / ライセンス: MIT