1

I can't get Python to print a word doc. What I am trying to do is to open the Word document, print it and close it. I can open Word and the Word document:

import win32com.client

msword = win32com.client.Dispatch("Word.Application") 
msword.Documents.Open("X:\Backoffice\Adam\checklist.docx")

msword.visible= True

I have tried next to print

msword.activedocument.printout("X:\Backoffice\Adam\checklist.docx")

I get the error of "print out not valid".

Could someone shed some light on this how I can print this file from Python. I think it might be as simple as changing the word "printout". Thanks, I'm new to Python.

2 Answers 2

2

msword.ActiveDocument gives you the current active document. The PrintOut method prints that document: it doesn't take a document filename as a parameter.

From http://msdn.microsoft.com/en-us/library/aa220363(v=office.11).aspx:

expression.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, 
  Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, 
  ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, 
  PrintZoomPaperHeight)

Specifically Word is trying to use your filename as a boolean Background which may be set True to print in the background.

Edit: Case matters and the error is a bit bizarre. msword.ActiveDocument.Printout() should print it. msword.ActiveDocument.printout() throws an error complaining that 'PrintOut' is not a property.

I think what happens internally is that Python tries to compensate when you don't match the case on properties but it doesn't get it quite right for methods. Or something like that anyway. ActiveDocument and activedocument are interchangeable but PrintOut and printout aren't.

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

4 Comments

Ok I know this is a dumb question but do I just need to enter range in to print all like
ms.word.activedocument.printout(range=wdprintalldocument) or need to enter in each character from above?
I get Exception occurred.', (0, 'Microsoft Word', "'PrintOut' is not a property."
I've edited my answer: you need to use PrintOut with camel case.
1

You probably have to escape the backslash character \ with \\:

msword.Documents.Open("X:\\Backoffice\\Adam\\checklist.docx")

EDIT: Explanation

The backslash is usually used to declare special characters. For example \n is the special character for a new-line. If you want a literal \ you have to escape it.

3 Comments

In addition, you may also use raw strings, where such special characters are not possible: "X:\\Backoffice\\Adam" would then be equal to r"X:\Backoffice\Adam". This is quite useful to permit copy-pasting.
Thanks I can get it to open, it is getting it print that I can't accomplish
Word opens and the file opens, but the print function wont execute?

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.