{"data":{"slug":"github-claude-office-skills-skills-docx-manipulation-ef937cb24ba2","title":"DOCX Manipulation for Document Customization","summary":"Create, edit, and format Word documents with precise control over structure, styling, and content using the python-docx library. Generate professional reports, add headers and footers, and manipulate tables and images programmatically.","url":"https:\/\/skillshunter.net\/skills\/github-claude-office-skills-skills-docx-manipulation-ef937cb24ba2","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-claude-office-skills-skills-docx-manipulation-ef937cb24ba2","markdown_url":"https:\/\/skillshunter.net\/skills\/github-claude-office-skills-skills-docx-manipulation-ef937cb24ba2.md","download_url":"https:\/\/skillshunter.net\/skills\/github-claude-office-skills-skills-docx-manipulation-ef937cb24ba2\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Install the python-docx library using 'pip install python-docx' before executing any code. Ensure that the source document is accessible and compatible with the library's formatting capabilities.","instructions":"---\n# \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\n# CLAUDE OFFICE SKILL - Enhanced Metadata v2.0\n# \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\n\n# Basic Information\nname: docx-manipulation\ndescription: \"Create, edit, and manipulate Word documents programmatically using python-docx\"\nversion: \"1.0\"\nauthor: claude-office-skills\nlicense: MIT\n\n# Categorization\ncategory: document\ntags:\n  - docx\n  - word\n  - manipulation\n  - editing\ndepartment: All\n\n# AI Model Compatibility\nmodels:\n  recommended:\n    - claude-sonnet-4\n    - claude-opus-4\n  compatible:\n    - claude-3-5-sonnet\n    - gpt-4\n    - gpt-4o\n\n# MCP Tools Integration\nmcp:\n  server: office-mcp\n  tools:\n    - create_docx\n    - extract_text_from_docx\n    - fill_docx_template\n    - merge_docx_files\n\n# Skill Capabilities\ncapabilities:\n  - document_creation\n  - editing\n  - template_handling\n\n# Language Support\nlanguages:\n  - en\n  - zh\n---\n\n# DOCX Manipulation Skill\n\n## Overview\n\nThis skill enables programmatic creation, editing, and manipulation of Microsoft Word (.docx) documents using the **python-docx** library. Create professional documents with proper formatting, styles, tables, and images without manual editing.\n\n## How to Use\n\n1. Describe what you want to create or modify in a Word document\n2. Provide any source content (text, data, images)\n3. I'll generate python-docx code and execute it\n\n**Example prompts:**\n- \"Create a professional report with title, headings, and a table\"\n- \"Add a header and footer to this document\"\n- \"Generate a contract document with placeholders\"\n- \"Convert this markdown content to a styled Word document\"\n\n## Domain Knowledge\n\n### python-docx Fundamentals\n\n```python\nfrom docx import Document\nfrom docx.shared import Inches, Pt, Cm\nfrom docx.enum.text import WD_ALIGN_PARAGRAPH\nfrom docx.enum.style import WD_STYLE_TYPE\n\n# Create new document\ndoc = Document()\n\n# Or open existing\ndoc = Document('existing.docx')\n```\n\n### Document Structure\n```\nDocument\n\u251c\u2500\u2500 sections (margins, orientation, size)\n\u251c\u2500\u2500 paragraphs (text with formatting)\n\u251c\u2500\u2500 tables (rows, cells, merged cells)\n\u251c\u2500\u2500 pictures (inline images)\n\u2514\u2500\u2500 styles (predefined formatting)\n```\n\n### Adding Content\n\n#### Paragraphs & Headings\n```python\n# Add heading (level 0-9)\ndoc.add_heading('Main Title', level=0)\ndoc.add_heading('Section Title', level=1)\n\n# Add paragraph\npara = doc.add_paragraph('Normal text here')\n\n# Add styled paragraph\ndoc.add_paragraph('Note: Important!', style='Intense Quote')\n\n# Add with inline formatting\npara = doc.add_paragraph()\npara.add_run('Bold text').bold = True\npara.add_run(' and ')\npara.add_run('italic text').italic = True\n```\n\n#### Tables\n```python\n# Create table\ntable = doc.add_table(rows=3, cols=3)\ntable.style = 'Table Grid'\n\n# Add content\ntable.cell(0, 0).text = 'Header 1'\ntable.rows[0].cells[1].text = 'Header 2'\n\n# Add row dynamically\nrow = table.add_row()\nrow.cells[0].text = 'New data'\n\n# Merge cells\na = table.cell(0, 0)\nb = table.cell(0, 2)\na.merge(b)\n```\n\n#### Images\n```python\n# Add image with size\ndoc.add_picture('image.png', width=Inches(4))\n\n# Add to specific paragraph\npara = doc.add_paragraph()\nrun = para.add_run()\nrun.add_picture('logo.png', width=Inches(1.5))\n```\n\n### Formatting\n\n#### Paragraph Formatting\n```python\nfrom docx.enum.text import WD_ALIGN_PARAGRAPH\nfrom docx.shared import Pt, Inches\n\npara = doc.add_paragraph('Formatted text')\npara.alignment = WD_ALIGN_PARAGRAPH.CENTER\npara.paragraph_format.line_spacing = 1.5\npara.paragraph_format.space_after = Pt(12)\npara.paragraph_format.first_line_indent = Inches(0.5)\n```\n\n#### Character Formatting\n```python\nrun = para.add_run('Styled text')\nrun.bold = True\nrun.italic = True\nrun.underline = True\nrun.font.name = 'Arial'\nrun.font.size = Pt(14)\nrun.font.color.rgb = RGBColor(0x00, 0x00, 0xFF)  # Blue\n```\n\n#### Page Setup\n```python\nfrom docx.enum.section import WD_ORIENT\nfrom docx.shared import Inches\n\nsection = doc.sections[0]\nsection.page_width = Inches(11)\nsection.page_height = Inches(8.5)\nsection.orientation = WD_ORIENT.LANDSCAPE\nsection.left_margin = Inches(1)\nsection.right_margin = Inches(1)\n```\n\n### Headers & Footers\n```python\nsection = doc.sections[0]\n\n# Header\nheader = section.header\nheader.paragraphs[0].text = \"Company Name\"\nheader.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER\n\n# Footer with page numbers\nfooter = section.footer\npara = footer.paragraphs[0]\npara.text = \"Page \"\n# Add page number field\nrun = para.add_run()\nfldChar1 = OxmlElement('w:fldChar')\nfldChar1.set(qn('w:fldCharType'), 'begin')\nrun._r.append(fldChar1)\n# ... (field code for page number)\n```\n\n### Styles\n```python\n# Use built-in styles\ndoc.add_paragraph('Heading', style='Heading 1')\ndoc.add_paragraph('Quote', style='Quote')\ndoc.add_paragraph('List item', style='List Bullet')\n\n# Common styles:\n# - 'Normal', 'Heading 1-9', 'Title', 'Subtitle'\n# - 'Quote', 'Intense Quote', 'List Bullet', 'List Number'\n# - 'Table Grid', 'Light Shading', 'Medium Grid 1'\n```\n\n## Best Practices\n\n1. **Structure First**: Plan document hierarchy before coding\n2. **Use Styles**: Consistent formatting via styles, not manual formatting\n3. **Save Often**: Call `doc.save()` periodically for large documents\n4. **Handle Errors**: Check file existence before opening\n5. **Clean Up**: Remove template placeholders after filling\n\n## Common Patterns\n\n### Report Template\n```python\ndef create_report(title, sections):\n    doc = Document()\n    doc.add_heading(title, 0)\n    doc.add_paragraph(f'Generated: {datetime.now()}')\n    \n    for section_title, content in sections.items():\n        doc.add_heading(section_title, 1)\n        doc.add_paragraph(content)\n    \n    return doc\n```\n\n### Table from Data\n```python\ndef add_data_table(doc, headers, rows):\n    table = doc.add_table(rows=1, cols=len(headers))\n    table.style = 'Table Grid'\n    \n    # Headers\n    for i, header in enumerate(headers):\n        table.rows[0].cells[i].text = header\n        table.rows[0].cells[i].paragraphs[0].runs[0].bold = True\n    \n    # Data rows\n    for row_data in rows:\n        row = table.add_row()\n        for i, value in enumerate(row_data):\n            row.cells[i].text = str(value)\n    \n    return table\n```\n\n### Mail Merge Pattern\n```python\ndef fill_template(template_path, replacements):\n    doc = Document(template_path)\n    \n    for para in doc.paragraphs:\n        for key, value in replacements.items():\n            if f'{{{key}}}' in para.text:\n                para.text = para.text.replace(f'{{{key}}}', value)\n    \n    return doc\n```\n\n## Examples\n\n### Example 1: Create a Business Letter\n```python\nfrom docx import Document\nfrom docx.shared import Inches, Pt\nfrom docx.enum.text import WD_ALIGN_PARAGRAPH\nfrom datetime import datetime\n\ndoc = Document()\n\n# Letterhead\ndoc.add_paragraph('ACME Corporation')\ndoc.add_paragraph('123 Business Ave, Suite 100')\ndoc.add_paragraph('New York, NY 10001')\ndoc.add_paragraph()\n\n# Date\ndoc.add_paragraph(datetime.now().strftime('%B %d, %Y'))\ndoc.add_paragraph()\n\n# Recipient\ndoc.add_paragraph('Mr. John Smith')\ndoc.add_paragraph('XYZ Company')\ndoc.add_paragraph('456 Industry Blvd')\ndoc.add_paragraph('Chicago, IL 60601')\ndoc.add_paragraph()\n\n# Salutation\ndoc.add_paragraph('Dear Mr. Smith,')\ndoc.add_paragraph()\n\n# Body\nbody = \"\"\"We are pleased to inform you that your proposal has been accepted...\n\n[Letter body continues...]\n\nThank you for your continued partnership.\"\"\"\n\nfor para_text in body.split('\\n\\n'):\n    doc.add_paragraph(para_text)\n\ndoc.add_paragraph()\ndoc.add_paragraph('Sincerely,')\ndoc.add_paragraph()\ndoc.add_paragraph()\ndoc.add_paragraph('Jane Doe')\ndoc.add_paragraph('CEO, ACME Corporation')\n\ndoc.save('business_letter.docx')\n```\n\n### Example 2: Create a Report with Table\n```python\nfrom docx import Document\nfrom docx.shared import Inches\n\ndoc = Document()\ndoc.add_heading('Q4 Sales Report', 0)\n\n# Executive Summary\ndoc.add_heading('Executive Summary', 1)\ndoc.add_paragraph('Q4 2024 showed strong growth across all regions...')\n\n# Sales Table\ndoc.add_heading('Regional Performance', 1)\n\ntable = doc.add_table(rows=1, cols=4)\ntable.style = 'Medium Grid 1 Accent 1'\n\nheaders = ['Region', 'Q3 Sales', 'Q4 Sales', 'Growth']\nfor i, header in enumerate(headers):\n    table.rows[0].cells[i].text = header\n\ndata = [\n    ['North America', '$1.2M', '$1.5M', '+25%'],\n    ['Europe', '$800K', '$950K', '+18%'],\n    ['Asia Pacific', '$600K', '$750K', '+25%'],\n]\n\nfor row_data in data:\n    row = table.add_row()\n    for i, value in enumerate(row_data):\n        row.cells[i].text = value\n\ndoc.save('sales_report.docx')\n```\n\n## Limitations\n\n- Cannot execute macros or VBA code\n- Complex templates may lose some formatting\n- Limited support for advanced features (SmartArt, Charts)\n- No direct PDF conversion (use separate tool)\n- Track changes reading is limited\n\n## Installation\n\n```bash\npip install python-docx\n```\n\n## Resources\n\n- [python-docx Documentation](https:\/\/python-docx.readthedocs.io\/)\n- [GitHub Repository](https:\/\/github.com\/python-openxml\/python-docx)\n- [Office Open XML Spec](https:\/\/docs.microsoft.com\/en-us\/office\/open-xml\/open-xml-sdk)\n","use_cases":["Generate a professional report with title, headings, and a table","Add a header and footer to an existing document","Convert markdown content to a styled Word document","Merge cells in a table and add formatted text"],"limitations":[],"compatibility":[],"setup_tip":"```bash\npip install python-docx\n```","sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/claude-office-skills\/skills\/blob\/9c4c7d5cd2813a8936bf2c9fdb174ea883b85a11\/docx-manipulation\/SKILL.md","source_commit":"9c4c7d5cd2813a8936bf2c9fdb174ea883b85a11","license":"MIT","version":null,"listing_approved_at":"2026-11-22","created_at":"2026-11-21T13:36:26.000000Z","updated_at":"2026-11-22T11:17:55.000000Z","seo_title":"DOCX Manipulation for Document Customization","seo_description":"Create and edit Word documents with python-docx. Format text, add tables, images, and headers with ease.","seo_keywords":["docx manipulation","word document editing","python-docx formatting","table creation in docx","add header footer docx"]}}