如何使用命令行 (CLI) 对 Word 文档执行字符串替换
我有一个 Word 文档模板,需要对其进行查找和替换。 例如,我需要将
替换为 1 Test Street
。我尝试使用可以从命令行运行的宏来执行此操作,但您似乎无法传递参数,因此无法编辑您正在搜索的模式或要替换它的内容。
我最终需要使用 PHP,所以一直在寻找一些我可以用 exec 调用的东西,我确实找到了一个名为 PHPWord 的库,它看起来非常有用,但它的工作方式是将 .doc 文件解析为 xml 文件,这无法正常工作,导致单词被分成两半,因此对 str_replace 没有好处。
最后我考虑使用 VBScript,我对此完全没有经验,无法让它工作。
I have a word document template that I need to do a find and replace on.
For example I need to replace <address 1>
with 1 Test Street
.
I have tried doing this using a macro which I can run from command line but you don't seem to be able to pass in arguments and therefore cannot edit either the pattern you are searching for or the thing to replace it with.
I need to it using PHP ultimately so had been looking for something I can just call with an exec, I did find a library called PHPWord that looked very useful but the way it worked was to parse the .doc file into an xml file and this wasn't working properly resulting in words being split in half and therefore no good for a str_replace.
Finally I looked at using VBScript, I have no experience with this at all and could not get it to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 PHPDocX,现在用 PHP 替换 Word 文档中现有的文本字符串并保留所有 Word 样式非常简单: 用 PHP 替换 Word 文档中的文本
由于 Word(OOXML 标准)将段落内容分解为不同的文本的方式,该过程的实现实际上要困难得多。例如,如果 Word 文档的原始编辑器引入了一些更正,或者即使他按下了后退键,则单个单词可能会被分解为具有自己标记的不同文本串。
PHPDocX 的 searchAndReplace 方法查找特定的文本字符串,提取其 Word 样式,并用保留所有原始样式的替换文本重建相应的段落。
人们可以选择搜索和替换整个文档,包括页眉、页脚、脚注、注释等,或者如果性能很重要,则可以选择指定一部分。然而,该方法相当快,替换一百页Word文档中的文本字符串可能需要百分之几秒。
To replace an existing string of text in a Word document by another one with PHP, and perserving all Word styles, is now quite simple if you use PHPDocX: Replacing text in a Word document with PHP
This procedure is rather more difficult to implement that it may seem because of the way Word (OOXML standard) breaks the paragraph contents into different runs of text. For example, a single word may be broken into different runs of text with tehir own markup if the original editor of the Word document has introduced some corrections or even if (s)he has hit the back key.
The searchAndReplace method of PHPDocX looks for a certain string of text, extracts its Word styles, and reconstruct the corresponding paragrah with the replacement text preserving all the original styles.
One may choose to search and replace the whole document, including headers, footers, footnotes, comments, etcetera, or if performance is important one may choose to specify a part. Nevertheless the method is rather fast and replacing a string of text in a one hundred page Word document may take a few hundredths of a second.
为什么不使用 Interop 来访问 Word 文档呢?
为此,您必须使用 .NET,并且可以创建一个控制台应用程序。
查看 互操作类命名空间和这是一个例子。
Why don't you use Interop to access the Word document.
You will have to use .NET for this, and you can create a console application.
Have a look at Interop Class Namespace and this for an example.
我对 php 或 VBScript 不太熟悉
该任务在 python 中很容易 ::
在命令行上运行文件 $python thisfile.py filename
I am not so familiar with php or VBScript
The task is easy in python ::
Run the file on command line as $python thisfile.py filename