Categories
COMPUTER SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Send A File To Printer For Print Using Python

In this article, we will learn how to send a file to printer for printing using Python. But you may be wondering which OS we will be working with right? Well, we will look at how to print files using Python for Windows, Mac OS as well as Linux. So we will be covering all 3 major operating systems that are out there!

Send A File To Printer
Send A File To Printer

Now that should be good for most cases right? Great! Then without further ado, let us start right away!

So how do we go here? Shall we start by taking a look at each OS? I think that is a good way to do this I guess. So let me start with Windows first. Alright?

How To Send A File To Printer In Windows?

So if you are using Windows OS, then you will need to use a special Python module called win32.

Wait a minute! What is this win32 module?

Python Win32 Module

You know, to use any hardware from an application program, you need to have OS APIs. Right? Because these APIs will give you access to the hardware on which the OS is running. But this also means that if you want to access an hardware like a printer, you need it as well!

Now the Python Win32 module is one that is used to give access to the Windows OS API! Got that? Hence the name “Win32”. Because it was written to give access to the Win32 APIs of Windows OS.

Now it all makes sense right?! So we will use just this module in Windows to access our printer. Go ahead and install win32 module on your Windows computer using the command:

pip install win32

So how will our Python code to send files to a printer on Windows look like? Take a look at it for yourself!

The Code

import win32print
p = win32print.OpenPrinter(your_printer_name)
job = win32print.StartDocPrinter(p, 1, ("Test Raw text to test the Printer's Raw text printing!", None, "RAW"))
win32print.StartPrinter(p)
win32print.WritePrinter(p, "Print Me Puhleeezzz!")
win32print.EndPagePrinter(p)

Now there is a lot going on over here But do not worry. I will explain to you what is happening here.

The first thing we need to do is to import our win32 module’s win32print library. After that we will first open the printer using the OpenPrinter API from the library. So far so good, right?

What we do next is to send a simple raw print test command. We can do that by using the StartDocPrinter API of the Win32print Python module.

Now once the raw print test is done, we get into the real crux of our problem. To print our actual text. And to do that, we will use the WritePrinter( ) API of the win32print library!

That is all there is to it for printing a file using Python code in Windows!

Now that was the case for printing in Python using a Windows computer. But how do we do that for a Mac OS or a Linux? After all these OS do not have Win32 APIs right?

Well you are right, but we don’t even need that! Wait what? Then how are we going to use the printer then? We will see how in our next section!

How To Send A File To Printer In Linux & Mac?

Fora computer running Linux or Mac (which uses POSIX APIs), calling a printer from Python is easy! Why is that?

Because in Linux & Mac, all the hardware are accessible as files! So if we want to access a printer connected to the computer, we just need to use lpr file.

So if we want to print a file called printMe.txt, we will just need to run the following code:

import os
os.system("lpr -P printer_name printMe.txt")

It is as simple as that! Now here is one more reason for you to use a Linux computer there. Right? 😉

So there you have it! This is how you can send a file to printer for printing on Windows, Mac or Linux computers. I hope this was useful for you.

But if you have any questions, do not hesitate to ask me in the comments below. I will be more than happy to help. Alright?

So until next time, have a great day!

Learn How To Get Current Timestamp In Python!