Windows
1.Download and install the Python win32 module, which includes the win32 printing resource.
2.Open a text editor and create a file named "winprint.py."
3.Enter the following code to begin the python script, which imports the printing libraries and creates a printing object:
import os, sys import win32print
p = win32print.OpenPrinter (printer_name)
Note that "printer_name" is simply a stand-in for the name of the actual printer on your system and must be specified.
4.Enter the following code on the next line to open a printing job and send text to the printer, where "data to print" represents the raw text to send to the printer:
job = win32print.StartDocPrinter (p, 1, ("test of raw data", None, "RAW"))
win32print.StartPagePrinter (p)
win32print.WritePrinter (p, "data to print")
win32print.EndPagePrinter (p)
Linux and MAC
1.Open a text editor and create a file named "linuxprint.py."
2.Enter the following code in the text editor to begin the file and import the "os" module, which allows interaction with the operating system:
! /usr/bin/python
import os
3.Enter the following code on the next line to execute a command that sends a text file to the printer. This requires that you save text into a file before printing:
os.system("lpr -P printer_name file_name.txt")
Note that "printer_name" represents the name of the printer you use on your system and will vary. "file_name.txt" is the name of the text file used for printing and will also vary.