How to perform string replacement on a word document using command line (CLI)
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 par开发者_运维技巧se 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.
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.
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.
I am not so familiar with php or VBScript
The task is easy in python ::
#!/usr/bin/python
import sys
if __name__ == '__main__':
filename = sys.argv[1]
f = open(filename,'r')
output = f.read().replace('<Address 1>','1 Test Street')
f.close()
f = open('filename','w')
f.write(output)
f.close()
Run the file on command line as $python thisfile.py filename
精彩评论