3

I have a simple batch file here which will print a word document from the command line.

"C:\Program Files\Microsoft Office\Office12\winword.exe" "p:\docs\daily checks.doc" /mFilePrintDefault /mFileExit

I am trying to place this into a python script, I have managed to get the document to open by using

subprocess.Popen('"C:\\Program Files\Microsoft Office\Office12\winword.exe"' '"P:\\docs\\daily checks.doc "')

I can't seem to get the /mFilePrintDefault /mFileExit part in the command, I have tried using +'"/mFilePrintDefault /mFileExit"' plus without the +, but then the document won't open.

Can you possibly help to see how I can print this word document, or is there a better way

Thanks in advance

2 Answers 2

9

This should work:

subprocess.Popen(["C:\\Program Files\Microsoft Office\Office12\winword.exe", "P:\\docs\\daily checks.doc", "/mFilePrintDefault", "/mFileExit"]).communicate()

Or, altenatively,

subprocess.Popen("'C:\\Program Files\Microsoft Office\Office12\winword.exe' 'P:\\docs\\daily checks.doc' /mFilePrintDefault /mFileExit", shell=True).communicate()

When you use shell=True the command is executed through a shell. This means that you have to pass a single string the same way as you would write the command in a shell, that is, with the quotes to prevent arguments with spaces to be splitted.

When you use shell=False (the default value), the command isn't executed through a shell. This means that you've to split the arguments yourself. The way you do this, is passing a list with all the arguments. In this case, no extra quoting is needed because the list elements already provide this information.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect that worked, thanks very much for the quick response and the help. Spot on!
The /mFileExit flag doesn't work for Word 2013. I need to close the file manually before my script continues.
0

try using the following:

subprocess.Popen('"C:\\Program Files\Microsoft Office\Office12\winword.exe" m"P:\\docs\\daily checks.doc" /mFilePrintDefault /mFileExit')

Popen expects a complete string or a list of args, basically type what you'd type into the shell into Popen and it will work.

the documentation on Popen states:

args should be a string, or a sequence of program arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.