home / skills / openclaw / skills / tiangong-wps-word-automation

tiangong-wps-word-automation skill

/skills/fadeloo/tiangong-wps-word-automation

This skill automates single-document Word/WPS tasks on Windows, boosting productivity by reading, editing, formatting, and exporting files via COM.

npx playbooks add skill openclaw/skills --skill tiangong-wps-word-automation

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
3.0 KB
---
name: wps-word-automation
description: Automate common Word/WPS document operations on Windows via COM (read text, replace, insert, headings, headers/footers, page breaks, merge, split, export to PDF/TXT, add/replace images). Use for single-document actions (no batch).
---

# WPS/Word Automation (Windows)

Use the bundled Python script to control Word or WPS via COM.

## Requirements

- Windows with **Microsoft Word** or **WPS Writer** installed.
- Python + **pywin32** (`python -m pip install pywin32`).

## Quick start

```bash
python {baseDir}/scripts/wps_word_automation.py read --input "C:\path\file.docx"
python {baseDir}/scripts/wps_word_automation.py replace --input "C:\path\file.docx" --find "旧" --replace "新" --save "C:\path\out.docx"
python {baseDir}/scripts/wps_word_automation.py export --input "C:\path\file.docx" --format pdf --output "C:\path\out.pdf"
```

## Commands

### read
Extract plain text.

```bash
python {baseDir}/scripts/wps_word_automation.py read --input "C:\path\file.docx" --output "C:\path\out.txt"
```

### replace
Find/replace text.

```bash
python {baseDir}/scripts/wps_word_automation.py replace --input "C:\path\file.docx" --find "old" --replace "new" --save "C:\path\out.docx"
```

### insert
Insert text at start/end.

```bash
python {baseDir}/scripts/wps_word_automation.py insert --input "C:\path\file.docx" --text "Hello" --where start --save "C:\path\out.docx"
```

### headings
Apply Heading 1/2/3 to matching lines.

```bash
python {baseDir}/scripts/wps_word_automation.py headings --input "C:\path\file.docx" --level 1 --prefix "# " --save "C:\path\out.docx"
```

### header-footer
Set header/footer text.

```bash
python {baseDir}/scripts/wps_word_automation.py header-footer --input "C:\path\file.docx" --header "标题" --footer "页脚" --save "C:\path\out.docx"
```

### page-break
Insert a page break at the end.

```bash
python {baseDir}/scripts/wps_word_automation.py page-break --input "C:\path\file.docx" --save "C:\path\out.docx"
```

### merge
Merge multiple docs into one.

```bash
python {baseDir}/scripts/wps_word_automation.py merge --inputs "a.docx" "b.docx" --output "merged.docx"
```

### split
Split by page ranges (e.g., "1-3,4-6").

```bash
python {baseDir}/scripts/wps_word_automation.py split --input "C:\path\file.docx" --pages "1-3,4-6" --outdir "C:\out"
```

### export
Export to PDF or TXT.

```bash
python {baseDir}/scripts/wps_word_automation.py export --input "C:\path\file.docx" --format pdf --output "C:\path\out.pdf"
python {baseDir}/scripts/wps_word_automation.py export --input "C:\path\file.docx" --format txt --output "C:\path\out.txt"
```

### image
Add or replace image at the end.

```bash
python {baseDir}/scripts/wps_word_automation.py image --input "C:\path\file.docx" --image "C:\path\img.png" --save "C:\path\out.docx"
```

## Notes

- If WPS is installed, try `--app wps`; otherwise default uses Word.
- Use `--visible true` if you need to watch the UI.
- Avoid batch usage; this skill is for single-document operations.

Overview

This skill automates common Word/WPS document tasks on Windows using the COM interface. It performs single-document operations like reading text, find-and-replace, inserting content, manipulating headers/footers, splitting/merging, exporting to PDF/TXT, and adding or replacing images. It works with Microsoft Word or WPS Writer via pywin32.

How this skill works

The script connects to Word or WPS through COM calls and drives document methods to inspect and modify content programmatically. It supports commands for reading plain text, performing targeted find-and-replace, inserting text or page breaks, applying heading styles, setting header/footer text, merging or splitting documents by page ranges, exporting formats, and adding images. Use the --app flag to choose WPS when present and --visible to show the UI during operations.

When to use it

  • You need to extract plain text from a single Word/WPS document.
  • You want to perform precise find-and-replace or insert content at start/end of a document.
  • You must export a document to PDF or TXT programmatically on Windows.
  • You need to merge or split one document into parts by page ranges.
  • You want to update headers/footers, add page breaks, or insert/replace images in one file.

Best practices

  • Run on Windows with Microsoft Word or WPS Writer installed and pywin32 available.
  • Operate on one document at a time; avoid using this for large batch processing.
  • Keep backups before modifying files; the tool can save output to a different path.
  • Test commands on a sample file using --visible true to observe changes before automating.
  • Use explicit paths and quoted arguments for files with spaces in CLI calls.

Example use cases

  • Export a contract.docx to contract.pdf for archiving via the export command.
  • Run a find-and-replace to update terminology across a policy document and save to a new file.
  • Insert a cover note at the start of a report and add a page break before the body.
  • Merge multiple chapters into a single manuscript using the merge command.
  • Split a long PDF-source document into sections by page ranges and export each part to separate files.

FAQ

Does this work on Linux or macOS?

No. The skill uses Windows COM and requires Microsoft Word or WPS Writer on Windows.

Can I run operations on multiple files at once?

No. It is designed for single-document actions rather than batch processing; use a wrapper script if you need to iterate over many files.