I hope someone can help me with this. I have been searching for days trying to find a way to extract some text from a word document and update the document with some other text. This is not your standard search and replace functionality. This is what I mean. I have a word document that can have 1 to many occurrences of this: lat=29.78908972, long=-82.03912832. I need to start at the beginning of the document and extract the lat and lon values, pass those values to a Google service that will return the county associated with that lat/lon. I then need to update the document with the county. Repeat this until the end of the document. I also need to keep the word formatting. Everything I have found is seems to be of the search and replace variety, meaning, find this text and replace it with this text. I have also explored using bookmarks but because the lat/lon values are in a loop (in the document build), it only updated the last bookmark, not each bookmark. I guess because they were named the same? The document is actually a template that gets filled in from a third party app we are using called DeviceMagic. Any help would be GREATLY appreciated. Thanks.
1 Answer
If your Word file is in .docx format, and if the lat/lon values are in a format that can be reliably matched by a regular expression, then you could build a processing pipeline something like this:
- Use zip (or 7zip) to extract the XML file of the document text from the .docx file.
- Use grep to extract all unique lat/lon specifications to a text file, one per line.
- Write a bit of code in Python or the language of your choice to read the coordinates reverse-geocode them, and write out the results in the form of a sed script of text replacement commands that will replace each lat/lon specification with the corresponding county name.
- Use sed to run the sed script on the XML file of document text.
- Update the .docx archive with the modified XML file.
You could replace the use of grep and sed with all custom code if you prefer, though using existing tools like these will probably be simpler.