Community程式設計與開發github.com

docx - 專業文件建立、編輯與分析

支援追蹤修訂、註解、格式保留及文字擷取的完整文件建立、編輯與分析功能。當需要處理 .docx 專業文件時使用。

docx - 專業文件建立、編輯與分析 是什麼?

此技能提供一套完整的工具與工作流程,用於處理 Microsoft Word (.docx) 文件。它涵蓋三個核心領域:建立新文件、編輯現有文件,以及分析文件內容。對於建立新文件,技能推薦使用 docx-js(JavaScript/TypeScript 函式庫)來產生結構化的 Word 文件。對於編輯,技能提供了兩種主要途徑:一種是透過 Document Library (Python) 進行基本 OOXML 編輯,另一種是針對法律、學術或商業文件強烈建議的「紅線標記工作流程」,此流程會使用 OOXML 的追蹤修訂功能,記錄所有變更。分析功能則包括使用 pandoc 將文件轉換為 Markdown 以擷取文字,或直接存取解壓縮後的 XML 原始內容,以取得註解、格式或媒體等詳細資訊。該技能也支援將文件轉換為圖片用於視覺化分析。它適用於 Claude Code、Cursor 或任何可執行 shell 命令與 Python/Node.js 腳本的 AI 代理平台。其工作流程設計強調系統性:完整閱讀相關指南、解壓縮文件、批次實作變更、打包文件,並進行最終驗證。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComposioHQ/awesome-claude-skills/tree/master/document-skills/docx

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

DOCX 建立、編輯與分析

概述

使用者可能會要求你建立、編輯或分析 .docx 檔案的內容。.docx 檔案本質上是一個包含 XML 檔案和其他資源的 ZIP 壓縮檔,你可以對其進行讀取或編輯。針對不同的任務,你擁有不同的工具和工作流程。

工作流程決策樹

讀取/分析內容

請使用下方的「文字擷取」或「原始 XML 存取」章節

建立新文件

請使用「建立新的 Word 文件」工作流程

編輯現有文件

  • 你自己的文件 + 簡單變更 使用「基本 OOXML 編輯」工作流程

  • 別人的文件 使用 「紅線標記工作流程」(建議預設)

  • 法律、學術、商業或政府文件 使用 「紅線標記工作流程」(必要)

讀取與分析內容

文字擷取

如果你只需要讀取文件的文字內容,你應該使用 pandoc 將文件轉換為 Markdown。Pandoc 在保留文件結構方面表現出色,並且可以顯示追蹤修訂:

# 將文件轉換為 Markdown,並保留追蹤修訂
pandoc --track-changes=all path-to-file.docx -o output.md
# 選項: --track-changes=accept/reject/all

原始 XML 存取

你需要原始 XML 來存取:註解、複雜格式、文件結構、嵌入媒體和中繼資料。針對這些功能,你需要將文件解壓縮並讀取其原始 XML 內容。

解壓縮檔案

python ooxml/scripts/unpack.py <office_file> <output_directory>

關鍵檔案結構

  • word/document.xml - 主要文件內容
  • word/comments.xml - document.xml 中引用的註解
  • word/media/ - 嵌入的圖片與媒體檔案
  • 追蹤修訂使用 <w:ins>(插入)和 <w:del>(刪除)標籤

建立新的 Word 文件

從頭建立新的 Word 文件時,請使用 docx-js,它允許你使用 JavaScript/TypeScript 建立 Word 文件。

工作流程

  1. 必須 - 完整閱讀檔案:從頭到尾完整閱讀 docx-js.md(約 500 行)。讀取此檔案時,絕對不要設定任何範圍限制。 在繼續建立文件之前,請閱讀完整的檔案內容以了解詳細語法、關鍵格式規則和最佳實務。
  2. 使用 Document、Paragraph、TextRun 元件建立一個 JavaScript/TypeScript 檔案(你可以假設所有依賴項都已安裝,但如果沒有,請參考下方的依賴項章節)
  3. 使用 Packer.toBuffer() 匯出為 .docx

編輯現有的 Word 文件

編輯現有的 Word 文件時,請使用 Document Library(一個用於 OOXML 操作的 Python 函式庫)。此函式庫會自動處理基礎架構設定,並提供文件操作方法。對於複雜場景,你可以透過該函式庫直接存取底層的 DOM。

工作流程

  1. 必須 - 完整閱讀檔案:從頭到尾完整閱讀 ooxml.md(約 600 行)。讀取此檔案時,絕對不要設定任何範圍限制。 閱讀完整的檔案內容以了解 Document Library API 和用於直接編輯文件檔案的 XML 模式。
  2. 解壓縮文件:python ooxml/scripts/unpack.py <office_file> <output_directory>
  3. 建立並執行一個使用 Document Library 的 Python 腳本(請參閱 ooxml.md 中的「Document Library」章節)
  4. 打包最終文件:python ooxml/scripts/pack.py <input_directory> <office_file>

Document Library 同時提供用於常見操作的高階方法,以及用於複雜場景的直接 DOM 存取。

文件審閱的紅線標記工作流程

此工作流程允許你在實作 OOXML 之前,使用 Markdown 規劃全面的追蹤修訂。關鍵:要實現完整的追蹤修訂,你必須系統性地實作所有變更。

批次策略:將相關變更分組為 3-10 個變更的批次。這使得偵錯易於管理,同時保持效率。在進入下一批之前,測試每一批。

原則:精準的最小化編輯

在實作追蹤修訂時,只標記實際變更的文字。重複未變更的文字會使編輯更難審閱,且顯得不專業。將替換分解為:[未變更的文字] + [刪除] + [插入] + [未變更的文字]。透過從原始文件中提取 <w:r> 元素並重複使用它,來保留未變更文字的原始 RSID。

範例 - 將句子中的「30 days」改為「60 days」:

# 錯誤 - 替換整個句子
'<w:del><w:r><w:delText>The term is 30 days.</w:delText></w:r></w:del><w:ins><w:r><w:t>The term is 60 days.</w:t></w:r></w:ins>'

# 正確 - 僅標記變更的部分,保留未變更文字的原始 <w:r>
'<w:r w:rsidR="00AB12CD"><w:t>The term is </w:t></w:r><w:del><w:r><w:delText>30</w:delText></w:r></w:del><w:ins><w:r><w:t>60</w:t></w:r></w:ins><w:r w:rsidR="00AB12CD"><w:t> days.</w:t></w:r>'

追蹤修訂工作流程

  1. 取得 Markdown 表示:將文件轉換為保留追蹤修訂的 Markdown:

    pandoc --track-changes=all path-to-file.docx -o current.md
    
  2. 識別並分組變更:審閱文件並識別所有需要的變更,將它們組織成邏輯批次:

    定位方法(用於在 XML 中尋找變更):

    • 章節/標題編號(例如,「Section 3.2」、「Article IV」)
    • 段落識別碼(如果有編號)
    • 使用唯一周圍文字的 Grep 模式
    • 文件結構(例如,「第一個段落」、「簽名區塊」)
    • 請勿使用 Markdown 行號 - 它們無法對應到 XML 結構

    批次組織(每批分組 3-10 個相關變更):

    • 依章節:「批次 1:第 2 節修正」、「批次 2:第 5 節更新」
    • 依類型:「批次 1:日期修正」、「批次 2:當事人名稱變更」
    • 依複雜度:從簡單的文字替換開始,然後處理複雜的結構變更
    • 依序:「批次 1:頁 1-3」、「批次 2:頁 4-6」
  3. 閱讀文件並解壓縮:

    • 必須 - 完整閱讀檔案:從頭到尾完整閱讀 ooxml.md(約 600 行)。絕對不要設定任何範圍限制。 特別注意「Document Library」和「Tracked Change Patterns」章節。
    • 解壓縮文件:python ooxml/scripts/unpack.py <file.docx> <dir>
    • 記下建議的 RSID:解壓縮腳本會建議一個 RSID 供你在追蹤修訂中使用。複製此 RSID 以供步驟 4b 使用。
  4. 分批實作變更:將變更邏輯分組(依章節、類型或鄰近性),並在單一腳本中一起實作。這種方法:

    • 使偵錯更容易(較小的批次 = 更容易隔離錯誤)
    • 允許漸進式進展
    • 保持效率(批次大小為 3-10 個變更效果良好)

    建議的批次分組:

    • 依文件章節(例如,「第 3 節變更」、「定義」、「終止條款」)
    • 依變更類型(例如,「日期變更」、「當事人名稱更新」、「法律術語替換」)
    • 依鄰近性(例如,「頁 1-3 的變更」、「文件前半部的變更」)

    對於每一批相關變更:

    a. 將文字對應到 XML:在 word/document.xml 中 Grep 文字,以驗證文字是如何跨 <w:r> 元素分割的。

    b. 建立並執行腳本:使用 get_node 尋找節點、實作變更,然後執行 doc.save()。請參閱 ooxml.md 中的 「Document Library」 章節以了解模式。

    注意:在撰寫腳本之前,務必立即 Grep word/document.xml 以取得當前行號並驗證文字內容。每次腳本執行後,行號都會改變。

  5. 打包文件:完成所有批次後,將解壓縮的目錄轉換回 .docx:

    python ooxml/scripts/pack.py unpacked reviewed-document.docx
    
  6. 最終驗證:對完整文件進行全面檢查:

    • 將最終文件轉換為 Markdown:
      pandoc --track-changes=all reviewed-document.docx -o verification.md
      
    • 驗證所有變更是否正確套用:
      grep "原始詞彙" verification.md  # 應該找不到
      grep "取代詞彙" verification.md  # 應該找得到
      
    • 檢查是否引入了非預期的變更

將文件轉換為圖片

若要視覺化分析 Word 文件,請透過兩步驟流程將其轉換為圖片:

  1. 將 DOCX 轉換為 PDF:

    soffice --headless --convert-to pdf document.docx
    
  2. 將 PDF 頁面轉換為 JPEG 圖片:

    pdftoppm -jpeg -r 150 document.pdf page
    

    這會建立 page-1.jpg、page-2.jpg 等檔案。

選項:

  • -r 150:設定解析度為 150 DPI(可調整以平衡品質/大小)
  • -jpeg:輸出 JPEG 格式(如果需要 PNG 可使用 -png)
  • -f N:要轉換的起始頁面(例如,-f 2 從第 2 頁開始)
  • -l N:要轉換的結束頁面(例如,-l 5 在第 5 頁停止)
  • page:輸出檔案的字首

指定範圍的範例:

pdftoppm -jpeg -r 150 -f 2 -l 5 document.pdf page  # 僅轉換第 2-5 頁

程式碼風格指南

重要:產生 DOCX 操作的程式碼時:

  • 撰寫簡潔的程式碼
  • 避免冗長的變數名稱和多餘的操作
  • 避免不必要的 print 陳述式

依賴項

需要的依賴項(如果不存在則安裝):

  • pandoc:sudo apt-get install pandoc(用於文字擷取)
  • docx:npm install -g docx(用於建立新文件)
  • LibreOffice:sudo apt-get install libreoffice(用於 PDF 轉換)
  • Poppler:sudo apt-get install poppler-utils(用於 pdftoppm 將 PDF 轉換為圖片)
  • defusedxml:pip install defusedxml(用於安全的 XML 解析)

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ably-automation

Automate Ably tasks via Rube MCP (Composio). Always search tools first for current schemas.

abstract-automation

Automate Abstract tasks via Rube MCP (Composio). Always search tools first for current schemas.

abuselpdb-automation

Automate Abuselpdb tasks via Rube MCP (Composio). Always search tools first for current schemas.

abyssale-automation

Automate Abyssale tasks via Rube MCP (Composio). Always search tools first for current schemas.

accelo-automation

Automate Accelo tasks via Rube MCP (Composio). Always search tools first for current schemas.

accredible-certificates-automation

Automate Accredible Certificates tasks via Rube MCP (Composio). Always search tools first for current schemas.

acculynx-automation

Automate Acculynx tasks via Rube MCP (Composio). Always search tools first for current schemas.

active-campaign-automation

Automate ActiveCampaign tasks via Rube MCP (Composio). Always search tools first for current schemas.

addresszen-automation

Automate Addresszen tasks via Rube MCP (Composio). Always search tools first for current schemas.

adobe-automation

Automate Adobe tasks via Rube MCP (Composio). Always search tools first for current schemas.

adrapid-automation

Automate Adrapid tasks via Rube MCP (Composio). Always search tools first for current schemas.

adyntel-automation

Automate Adyntel tasks via Rube MCP (Composio). Always search tools first for current schemas.

aeroleads-automation

Automate Aeroleads tasks via Rube MCP (Composio). Always search tools first for current schemas.

aero-workflow-automation

Automate Aero Workflow tasks via Rube MCP (Composio). Always search tools first for current schemas.

affinda-automation

Automate Affinda tasks via Rube MCP (Composio). Always search tools first for current schemas.

affinity-automation

Automate Affinity tasks via Rube MCP (Composio). Always search tools first for current schemas.

agencyzoom-automation

Automate Agencyzoom tasks via Rube MCP (Composio). Always search tools first for current schemas.

agent-mail-automation

Automate Agent Mail tasks via Rube MCP (Composio). Always search tools first for current schemas.

agentql-automation

Automate Agentql tasks via Rube MCP (Composio). Always search tools first for current schemas.

agenty-automation

Automate Agenty tasks via Rube MCP (Composio). Always search tools first for current schemas.

相關技能