如何使用IronWord与ChatGPT Codex

Curtis Chau
Curtis Chau
更新: 2026年10月19日

当公司更改名称时,法律团队会收到一个已签名合同的文件夹以进行更新。 旧名称可以在正文段落、参与方表格、大写标题和页面页眉中找到。 Word还可以将名称存储在多个格式化运行中,例如,当其中一个片段在多年以前加粗时。 对每个文件进行标准的查找和替换只解决了一些情况,而其他情况下没有警告地保持不变。

ChatGPT Codex是来自OpenAI的编码代理。 在一个存储库中,它编辑文件,执行如dotnet run的命令,然后总结更改。 一个代理为终端、IDE、ChatGPT桌面应用程序和云任务服务。 IronWord是来自Iron Software的.NET库,它将现有的Word文档作为段落、运行和表格的对象模型打开,代码可以编辑和保存而无需Microsoft Office。

Codex在合约积压中与一个真实情况清单一起工作。批次提示以每个合约一行的形式写入CSV,因此Codex从中报告的每个计数都由文件支持。

快速入门
NuGet使用NuGet安装

PM > Install-Package IronWord

通过在 NuGet 包管理器控制台中运行上述命令安装 IronWord,或在 NuGet 包管理器中搜索该包。

在包含合约文件夹的.NET控制台项目中启动Codex,并在任何编辑前请求库存。

$ironword Add the IronWord 2026.10.4 NuGet package to this console project.
Then have Program.cs open every .docx in Contracts/sample/ without changing it.

For each file, write one row to out/quickstart.csv with these columns:

- file
- body count, how often "Northwind Traders" appears in the body text
- table count, the same name counted separately in every table cell
- the exception type, when the file fails to open

Print the totals by reading that CSV back.
Program.cs reads the key from IRONWORD_LICENSE_KEY itself.
Don't print the key's value, and don't list environment variables.
Nothing gets saved here, so an unset key is no reason to stop.
Build and run it with at most three repair attempts, then tell me the result.
Text


什么是ChatGPT Codex,它如何适用于.NET Word文档工作?

安装步骤在Codex CLI文档中,发布在开源库中。

Codex通过训练数据和提供的文件了解IronWord,而新版本可能会让双方过时。 2026.10版本通过流和异步调用加载和保存。

如何编写IronWord提示并为其设置Codex?

一个有效的批次提示命名输入、名称的每个位置、输出、证明、只读的真实情况和约束条件。

OpenAI的关于语言模型幻想的论文将自信的错误答案追溯到奖励猜测的训练和评估中,在一个重命名工作中,猜测是每个合约都是干净的。

Codex在工作目录和存储库根目录之间的每个目录中的.agents/skills中寻找技能。 保存为.agents/skills/IronWord/SKILL.md,IronWord的skill.md解析$ironword提及。

在任何工作之前,Codex在存储库根目录和工作文件夹之间的路径中每个目录中最多读取一个AGENTS.md。 距离工作文件夹最近的文件优先。 /init命令起草一个起始AGENTS.md。

Give this repository a top-level AGENTS.md with a Contract rebrand section.
List these rules in it:

- No DOCX library other than IronWord is allowed.
- IronWord's package is IronWord, and its namespaces are IronWord and IronWord.Models.
- The ironword skill in .agents/skills/ironword/ is one API reference.
- https://ironsoftware.com/word/csharp/llms.txt is the other.
- Contracts/inbox/ and Contracts/manifest.csv are read-only.
- Rebranded copies go to out/rebranded/ under their original names.
- IRONWORD_LICENSE_KEY holds the key, and its value never appears in output, logs or files.
- Checking the variable means its presence and length, nothing more.
- Any count you report is read from a CSV written in the same turn.
- The reply names that CSV.
Text

如何请求Codex添加IronWord并检查许可证?

Print the IronWord PackageReference from ContractRebrand.csproj and the version restore resolved.
Then quote every IronSoftware entry that dotnet list package --include-transitive prints.
Leave the package references exactly as they are.
Text

IronWord 2026.10.4引用了IronSoftware.Abstractions 2026.1.2、IronSoftware.Common 2026.4.2和IronSoftware.System.Drawing 2026.11.3。还原列出的IronSoftware.Drawing.Abstractions 2026.1.2。构建将Windows、Linux和macOS的本地LayoutEngineInterop库复制到runtimes文件夹中。 OpenXML冲突页面涵盖了2025.7.13之前发行版中的一个与更新的DocumentFormat.OpenXml的冲突。在固定之前检查IronWord变更日志。

没有密钥时,打开、读取和替换成功,但每个保存过载(包括异步)都引发LicensingException并显示消息需要Production License。

Add Startup.ApplyIronWordKey and call it first in Program.cs.
It copies the IRONWORD_LICENSE_KEY value into IronWord.License.LicenseKey.
It also reports whether License.IsLicensed is true.

Add a --dry-run switch that opens and edits in memory and never saves.
Then run the sample twice with the variable removed:

1. Run it once with --dry-run.
2. Run it once copying each file to out/licence-check/ through SaveAs.

Clear the variable inside each child process only, never for the session.
Write out/licence-check.csv with the columns run, file, call, exception type name and message.
Tell me which calls threw.
The licensing exception type isn't public, so catch Exception and record its GetType().FullName.
Stop after three repair attempts.
Text

许可证密钥页面还涵盖了Web.config、App.config和appsettings.json。

Word文档的哪些部分可以进行文本替换?

Create Services/MentionScanner.cs.
For one DOCX it returns a count for each of these:

- "Northwind Traders" in body paragraphs
- "Northwind Traders" in table cells
- the name in upper case
- the name inside "Northwind Traders Pension Trust"
- the name in headers and footers
- the old notice address
- the Fax notices paragraph

The body and table cell counts leave out the pension trust name.
IronWord has no header or footer model.
For that column only, read word/header*.xml and word/footer*.xml with System.IO.Compression.

Then run it over Contracts/inbox/ into out/inventory.csv.
Mark files that fail to open as Unreadable.
List every file whose counts differ from manifest.csv.
In that comparison, the body count is body_mentions plus split_run_mentions.
Text

内置的浏览器代理在实际文件上测试替换。

Create a scratch console project under scratch/replace-probe/ yourself.
Add a Compile Remove line for scratch/** to ContractRebrand.csproj.

Then ask the explorer agent to use that project.
It finds which parts of a contract the document-level ReplaceText call changes.
It tests copies of the Contracts/sample/ files in memory, with these four cases:

- a name split across runs
- a table cell
- the upper-case title
- the pension trust

The header has no object in the API, so the explorer answers that one from IronWord.xml.
It changes nothing under Services/ or in Program.cs.

Write the five answers to out/replace-reach.csv.
Beside each answer, quote the ironword skill or IronWord documentation.
Where a page and the test disagree, say so.
Text

2026.10.4校准给出了这些内存中的结果。

位置文档级替换传递到它的内容
正文,一次运行已替换文档调用
正文,跨运行拆分已替换文档调用
表格单元未改变每个TableCell上的ReplaceText
大写标题未更改(区分大小写匹配)以大写形式进行的第二次调用
养老金信托名称重命名(普通子串匹配)占位符先替换后恢复
页眉或页脚在对象模型中不存在报告它,然后手动编辑

拆分运行行有一个副作用。 一个以粗体North开头然后是风Traders的名称变成一个读作Contoso Ltd的粗体运行。

一个基于文档ExtractText的检查仅返回正文段落,导致放行的合约的参与方表仍使用旧公司名称。 FindText因其仅匹配包含整个短语的文本元素,因此错过了拆分运行匹配。

如何替换整个文件夹中的文档文本?

Have the worker agent write Services/Rebrander.cs.
It applies every rule in Contracts/README.md to Contracts/inbox/.
It builds on the explorer's findings and follows these rules:

- Replace inside each table cell as well as the body.
- Keep the pension trust intact.
- Delete the Fax notices paragraph.
- Leave header and footer parts as they are, since IronWord can't edit them.
- Report those contracts as HeaderFlagged.
- Save each copy to out/rebranded/ with SaveAs.
- Never call Save() on a document opened from inbox/.

Add a --sequential mode that rescans the saved copies with MentionScanner.
It writes out/sequential.csv with these columns:

- file
- status from the README, always one of its four
- mentions left per location
- milliseconds
- header_lost, set to yes when a copy lacks a header or footer part its original had

The header_lost column is reported on its own and never changes a status.
Tell me the status counts.
Text

在IronWord 2026.10.4中,保存带有表格的加载DOCX会丢弃表内的每个文本编辑以及第一个表后任何段落中的编辑。 磁盘上的副本保留旧文本,不会引发异常,而内存中的文档仍显示替换。 第一个表之前的编辑保持,替换为新TableCell的单元也保持。 这些合约在参与方表之后放置签名,因此,在内存检查通过的情况下,保存的签名从未更改的副本仍会通过。

无参数的Save被记录为覆盖已加载文档的源文件,因此批处理不应调用它。 删除文本指南展示了传真规则背后的段落删除。

在校准中,加载零字节文件抛出NullReferenceException,而文本文件、截断包和随机字节包每个都从一个混淆的命名空间中抛出FileFormatException,并显示消息文件包含损坏的数据。 System.IO类型的捕获未捕获。 每个文件捕获Exception并记录它的类型。

拷贝使用SaveAs,这在IronWord的问题解决页面中被命名为在保存后消失的页眉。

Check whether SaveAs lost a header anywhere in this backlog.
IronWord's troubleshooting page header-disappears-after-saveas reports that in one environment.

For every HeaderFlagged file, compare the inbox original with its out/rebranded/ copy.
Look at the word/header*.xml and word/footer*.xml parts.
Write out/header-check.csv with these columns:

- file
- header parts before
- header parts after
- whether each part is present with its text unchanged

If a part went missing, rebrand that contract again from its inbox original.
Write it with the Save overload that takes the out/rebranded/ path.
Don't use the parameterless Save().
If the part is still missing, record it and stop.
Tell me which files lost a part.
Text

如何并行处理一文件夹的文档?

异步调用在不阻塞的情况下进行文件I/O,但文档指出解析和序列化仍是同步的,并且一个文档实例并非线程安全。

Add a --parallel mode that works like this:

- It runs Task.WhenAll over the inbox.
- Each task holds a slot of a SemaphoreSlim whose size comes from --concurrency.
- Then it awaits WordDocument.LoadAsync and SaveAsAsync on its own WordDocument.

Run the mode three times:

1. At concurrency 1 into out/parallel-c1/.
2. At concurrency 4 into out/parallel-c4/.
3. At Environment.ProcessorCount into out/parallel-max/.

Write out/parallel-compare.csv with the columns file, folder and a yes or no.
The yes or no says whether the extracted body and cell text match out/rebranded/.
Log each run as a new line of out/runs.csv.
Give it the mode, concurrency, elapsed seconds and match count from out/parallel-compare.csv.
Tell me the quickest setting in runs.csv.
Text

在校准中,内存中打开和编辑收件箱,包括四个不可读文件,顺序需要4.8秒,4个限制需要1.4秒,16个需要1.8秒,加上SaveAsAsync则分别增加到14.4、5.5和4.7秒。 保存是限制步骤。

如何诊断仍然显示旧文本的文档?

Codex接受通过-i标志(--image)在新会话或codex exec resume中或作为粘贴到编辑器中的图像。

I attached the first page of out/rebranded/MSA-1050.docx as an image with -i.
Its header still names Northwind Traders.
Look at the image, then explain from the file itself why the batch left that text.
Tell me whether out/sequential.csv already flags this file.
Don't change any code.
Text

正确的答案要指出页眉部分和文件的HeaderFlagged行。

如何从头到尾调试不正确的批处理输出?

Find out why the nightly job passed contracts that legal later flagged.
The log reports 0 contracts still naming Northwind Traders.
Contracts/legal-review.csv lists contracts where legal still found the old name.
It was in the party table or an upper-case title.
The file also lists a pension trust that was renamed.

Send the explorer agent through Jobs/NightlyRebrand.cs and Logs/nightly-2026-10-07/ first.
Then write your own report in out/nightly-diagnosis.md with these parts:

- why the job's own check passed them
- the smallest fix
- the command that proves the fix

Don't run or edit any code.
Text

诊断应引用标记合约干净的工作的行。

如何将一次性代码转化为可重用的、经过测试的组件?

Put the worker agent on a RebrandJob class, built like this:

- Install the Microsoft.Extensions.DependencyInjection package.
- Have its container resolve RebrandJob.
- Set it up through a RebrandOptions record with inbox, output folder, concurrency and dry run.
- Keep every README rule, the header check and the per-file CSV.
- RebrandJob.cs stays under 180 lines, with XML doc comments.

The README rules cover body and cell replacement, upper case and the pension trust guard.
They also cover the address and the fax deletion.
Run the job over the whole inbox into out/final/ and out/final.csv.
Then tell me whether every status matches manifest.csv.
Also confirm no copy keeps an old name, old address or fax paragraph.
Old names in a copy's header or in the pension trust don't count.
Text

重构可以移除一个规则并仍然成功构建。

Add an xUnit project at tests/ContractRebrand.Tests.
Exclude it from the main project's compile items, and join both projects in a solution file.

Write tests on real inbox files that check these:

- a split-run contract comes back with no old name
- a table contract is clean in its cells
- a pension trust mention survives
- a header contract is HeaderFlagged
- the zero-byte file is Unreadable without stopping the batch
- 20 files rebranded in parallel equal their sequential copies

Assert on extracted text and statuses, never on DOCX bytes.
Keep test copies in out/tests/work/.
Run dotnet test on the solution with a trx logger into out/tests/.
Report the totals from the trx file.
Text

OpenAI的自定义提示文档不推荐保存在~/.codex/prompts中的提示,而倾向于技能,其中重复出现的问题属于技能。

$ironword Look in the installed IronWord.xml.
List the WordDocument members that load or save a document, and answer these:

- Which ones are asynchronous?
- Which one overwrites the source file a document was loaded from?

List only members you can call from a line that compiles in a probe under scratch/.
Also name any member anywhere in IronWord.xml that is documented but doesn't compile.
Check by compiling only.
Don't run Save() on any document loaded from Contracts/inbox/.
Then show the line in RebrandJob that keeps Contracts/inbox/ untouched.
Text

如何在另一文件文档积压中重用这些提示?

更改名称、路径和规则后,这些提示适用于下一个积压,IronWord AI编码助手指南涵盖五个助手的设置。

在实际运行中这些提示生成了什么?

这些提示在一个Codex会话中按顺序运行。 .NET 9项目持有124份合约及预期状态和计数的清单,并且每个Release构建都成功完成。 我们的扫描仪重新扫描了每个已保存的副本,并且在一个新克隆中重新运行测试。

  • 在重命名之前,库存标记所有124个文件与清单匹配,并将4个非DOCX文件标记为不可读。
  • 在顺序重命名后,120份已保存副本中的参与方表上方的114个旧名称没有保留,22个跨运行的也包括在内。
  • 没有13个大写标题、38个旧通知地址或57个传真段落留下来。
  • 所有9个养老金信托名称保持不变。
  • 旧名称在28个参与方表和114个签名中仍然存在,因为保存丢弃了那些编辑。
  • worker's sequential.csv给予每个文件清单的状态,重命名104个,HeaderFlagged 10个,无变化6个,不可读4个,但其自身计数列仍显示114个签名和28个表格提及。 状态记录检测,而非替换,正如Codex本身报告。
  • 在10个示例文件中,快速启动报告19个主体和2个表提及,而清单中为16个和2个。其主体计数还包括2个单元提及和养老金信托名称。
  • 没有密钥时,干运行在内存中编辑了所有9个可读样本文件。 保存运行在每个文件的SaveAs中抛出LicensingException。
  • 在限制为1、4和32时,平行运行分别花费11.456、7.825和5.830秒。 每个文件夹中的所有120个可读副本与顺序的相匹配。
  • 每晚的诊断将发现追溯到工作的区分大小写ExtractText().Contains检查及其文档级替换。 它引用了没有行号,并且其证明命令针对了一个尚不存在的测试项目。
  • 重构将每个规则保留在一个68行RebrandJob.cs中,其out/最终副本匹配出/重命名的。
  • final.csv与清单状态匹配,但将118行标记为不完整,并且Codex报告内容检查失败。
  • Codex的运行和新的克隆各通过6个测试中的4个。 拆分运行测试在签名上失败,参与方表单元的表测试失败。
  • API答案列出了四个加载、七个保存和五个异步成员,并将无参数Save命名为覆盖源的成员。
  • 它正确命名了四个不可编译的文档成员。这些是接受字节和字符串的构造函数、FlushModelToBytes、段落上的ReplaceTextAcrossRuns和SetNoChangeStatusAsUpdated。
  • 它还列出了仅字节的构造函数,编译成功,遗漏了在2026.10.4上失败的其他五个,FromMarkdown、FromMarkdownFile、ExportToMarkdownFile、GetMailMergeWorkingBytes和表格上的ApplyCellStyle。
  • 有些步骤与页面中的运行不一样。 图像使用python-docx从运行的MSA-1050副本中呈现。 夜间工作、其日志和法律审查文件直接在调查提示前添加。

GitHub Copilot指南通过Tables、Rows和Cells读取其表来测试保存的语句。 Cursor指南列出了IronWord不读取的DOCX部分。

共享的运行记录列出了此运行的Codex版本、模型、机器和时间。


故障排除


有问题吗?

如果您有任何问题,请联系support@ironsoftware.com

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 62,392版本:2026.11刚刚发布