I have had a massive problem for years involving MS Word and templates. I have a set of “core document” templates that I create for every new case that I handle. I’ve wanted for years to figure out a method to automate populating these templates for each new case. There are some automation tools that exist on the market, but they are exceedingly expensive. After listening to episode 54 with Dr. Drang, I decided I was going to finally sit down and figure this out on my own. I discovered a way to do this in Python and, honestly, it could not have been easier to do. I’m thrilled because it will save me and (primarily) my assistant hours of needless repetitive work that the computer should be doing for us. It’s great!
But I now have three questions.
One of the fields in my MS Word merge file uses a forward slash. When I try to reference it and assign data to the field, I get an error from Python. I assume it thinks the forward slash means I want to divide. I’ve tried bracketing the field, which seems to be the way poorly formatted SQL columns are handled, but that didn’t do the trick. I tried using quotation marks, and I tried using the backslash as an escape character. Neither of those worked, either. I control the Word file, so I can change this field name. But I’d rather figure out how to be able to use it as is in Python (if possible).
My Mac has three versions of Python: 2.7x, 3.7, and 3.8.4. If I run the program from IDLE, it works perfectly. If I run it from the command line, I get errors. It seems the cause is that the shebang is referencing Python 2.7 in /usr/bin. Must I use “/usr/local/bin/Python3.8/” as the shebang in all of my python programs? And if so, what happens when Python updates to 3.9 or 4.0 or something else? Anything I can do to avoid hardwiring the Python version in my program?
I needed two packages to make this work. lmxl and docx-mailmerge. I was able to install them with pip. They seem to work just fine when I run my program from within IDLE or even the interactive interpreter from terminal; however, from the command line the interpreter can’t seem to access the mailmerge package. When I run pip from command line it confirms that the seemingly “missing” package has been installed.
Appreciate any help, ideas, or links elsewhere.
P.S. If these questions would be better on Stack Overflow or some place else, just say the word and I’ll ask this there.