Python Docxのロゴ

Python Docx

組織
cxcscmu
python-docx

Programmatically read, modify, and create Word documents (.docx) with python-docx library

概要

公開者cxcscmu
リポジトリSkillLearnBench
スキル名python-docx
スター
91
フォーク
7
同梱ファイル
指示のみ
ライセンスMIT
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    cxcscmuがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキル「Python Docx」をTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/cxcscmu/SkillLearnBench/tree/main/skills/b1-one-shot-claude-haiku-4-5/offer-letter-generator/python-docx
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/cxcscmu/SkillLearnBench.git /tmp/SkillLearnBench
mkdir -p .claude/skills
cp -r /tmp/SkillLearnBench/skills/b1-one-shot-claude-haiku-4-5/offer-letter-generator/python-docx .claude/skills/python-docx
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでPython Docxを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあるとすぐに、モデルが自動的にPython Docxを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Python Docxは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、スキルを使わない間は指示の読み込みにトークンを消費しません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Python-docx: Working with Word Documents

Overview

The python-docx library allows you to create, read, and update Microsoft Word (.docx) files programmatically. It's essential for automating document generation, template filling, and Word document manipulation in Python.

Installation

bash
pip install python-docx

Core Concepts

Document Structure

  • Document: Root object representing the entire .docx file
  • Paragraphs: Text blocks in the document
  • Runs: Individual text segments within a paragraph (can have different formatting)
  • Tables: Structured data with rows/cells
  • Sections: Document sections with different headers/footers

Common Operations

Loading and Saving Documents

python
from docx import Document

# Open existing document
doc = Document('template.docx')

# Create new document
doc = Document()

# Save document
doc.save('output.docx')

Working with Paragraphs and Text

python
# Access all paragraphs
for para in doc.paragraphs:
    print(para.text)

# Add new paragraph
new_para = doc.add_paragraph('Text here')

# Add text with formatting
run = new_para.add_run('Bold text')
run.bold = True

Finding and Replacing Text

python
# Search through all paragraphs and runs
for para in doc.paragraphs:
    for run in para.runs:
        if '{{PLACEHOLDER}}' in run.text:
            run.text = run.text.replace('{{PLACEHOLDER}}', 'replacement')

# Note: Text can be split across multiple runs!
# A safer approach:
full_text = ''.join(run.text for run in para.runs)
if '{{PLACEHOLDER}}' in full_text:
    # Clear all runs and recreate with replacement
    for run in para.runs:
        run.text = ''
    para.text = full_text.replace('{{PLACEHOLDER}}', 'replacement')

Working with Tables

python
# Access table rows and cells
for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            print(cell.text)
            # Replace text in cell
            cell.text = cell.text.replace('{{PLACEHOLDER}}', 'value')

Handling Multiple Runs

The trickiest part of python-docx is that text in a paragraph can be split across multiple "runs" (especially in templates). For placeholder replacement:

python
def replace_text_in_paragraph(para, old_text, new_text):
    """Replace text handling split runs"""
    if old_text in para.text:
        # Clear existing runs
        for run in para.runs:
            run.text = ''
        # Add new text as single run
        para.text = new_text

Important Considerations

  1. Text Fragmentation: When you open a .docx file (especially templates), the same logical text might be split across multiple run objects due to formatting. Always check the full paragraph text before manipulating individual runs.

  2. Preserve Formatting: When replacing text, consider whether you want to preserve the original formatting or apply new formatting.

  3. Iterating Safely: If modifying document structure while iterating, make copies of lists first:

    python
    paragraphs = list(doc.paragraphs)  # Create copy
    for para in paragraphs:
        # Safe to modify now
  4. Headers/Footers: Accessed via doc.sections[0].header and doc.sections[0].footer

Example: Template Filling

python
from docx import Document

def fill_template(template_path, replacements, output_path):
    doc = Document(template_path)

    # Replace in paragraphs
    for para in doc.paragraphs:
        for key, value in replacements.items():
            if key in para.text:
                para.text = para.text.replace(key, value)

    # Replace in tables
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                for key, value in replacements.items():
                    if key in cell.text:
                        cell.text = cell.text.replace(key, value)

    doc.save(output_path)

Debugging Tips

  • Always print para.text (full paragraph) to see actual content vs individual run.text
  • Check len(para.runs) to see if text is fragmented
  • Use doc.element.xml to inspect underlying XML structure if needed

よくある質問

AIスキル「Python Docx」では何ができますか?

Programmatically read, modify, and create Word documents (.docx) with python-docx library

TypingMindでPython Docxを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Python Docxはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにPython Docxをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/cxcscmu/SkillLearnBench/tree/main/skills/b1-one-shot-claude-haiku-4-5/offer-letter-generator/python-docxを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Python Docxを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストの内容がこのスキルの用途に合ったときに、このスキルを読み込めます。

Python Docxで使えるAIモデルの数に制限はありますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもPython Docxを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Python Docx」は無料ですか?

はい。cxcscmuがMITライセンスのもとでGitHubに公開しています。ご利用のAIプロバイダーに支払うのは、使用したトークンの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇