Python Forum

Full Version: How to print data to the printer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Does anyone know how to make your program print data from my program to the printer?
As an example you could use the following:
    printText = your_widget_namet.get("1.0", END)
    print(printText)
    filename = tempfile.mktemp(".txt")
    open(filename, "w").write(printText)
    # Bellow is call to print text from your_widget_name textbox
    win32api.ShellExecute(0,"printto", filename, '"%s"' % win32print.GetDefaultPrinter(), ".", 0)
OK. I'll try thanks.
That didn't work.
Please show your complete code and any errors received in the appropriate tags so it can be analysed, that will make it easier to see what you have done and what might be your issue.
I get the following error code: NameError: name 'tempfile' is not defined


printText =Label(root, text="hello")
print(printText)
filename = tempfile.mktemp(".txt")
open(filename, "w").write(printText)
# Bellow is call to print text from your_widget_name textbox
win32api.ShellExecute(0,"printto", filename, '"%s"' % win32print.GetDefaultPrinter(), ".", 0)
I'm getting closer but still not working. I went to that link and installed all components. But when I try running the following code, I get this error: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it


printer = getNetworkPrinter()(host='192.168.0.1', port=8080)

printer.text("Hello World")
printer.lf()
Are you going through a router? If so you may have to open a port to the printer.
Yes the printer goes through a router.
Pages: 1 2