Python win32 ShellExecute error 31: 'A device attached to the system is not functioning.'

Viewed 5511

I have a python program where I try to send a document to a printer. It works fine in my machine with a local printer set as the default printer and connected with my laptop via USB. However, when I tried it with another computer with its default printer being in a network it doesn't work. The error I get is:

pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')

The piece of code is giving problems is the following one:

win32api.ShellExecute(0,"print",doc_path,None,".",0)

I think the problem is because of the printer being connected using the network and not the USB, but it might be another thing.

Thanks for any help.

3 Answers

I found the cause of the error: the computer didn't have a pdf reader installed and that was causing that error (the file to print was a pdf).

Installing Foxit reader (and making it the default program to open pdfs) fixed the problem.

I've got this error when executing a simple print() command but in a command window with utf-8 enabled via the following commands:

chcp 65001

set PYTHONIOENCODING=UTF-8

This caused the error; probably because the string python tried to print on the terminal was in a different encodign than utf-8.

I had the same problem. Simply installing a pdf reader wasn't enough though, I had to make it the default program to open pdfs too to get it to work.

Related