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!

Categories
COMPUTER SCIENCE DATA SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Read Command Line Arguments In Python

In this article, we will see how to read the command line arguments passed to our Python program. So by the end of this article, you will know how to read the options sent to your program.

Sounds good? Great! Then let us start right away!

So in order for us to be able to read the parameters passed, we will make use of sys module. But before we look into this module, let us first see why want to pass arguments in the first place. Because knowing why we need it will help us understand how we can solve it later. I hope that makes sense!

What Are Command Line Arguments?

So let us start with what command line arguments are and then see how we can read them.

Say you want to write a Python program to add two numbers. Can you tell me what is the simplest code we can write for that? It looks something like this right?

a = 10
b = 20
sum = a + b
print(sum)

And when you run this piece of code, we get an output like this:

30

It is quite straight forward right? We just add the two numbers 10 & 20 and store it in the variable called sum. We then go ahead and print the value of the sum which is 30. So it is all good here. Right?

But there is one problem here. This program is quite worthless! Why? Because every time you run it, it always adds the numbers 10 & 20 to give us the result 30. Not so useful isn’t it?

Read Command Line Arguments
Read Command Line Arguments

So then what do we want? We want to write a Python program that can add two numbers. But we do not want these numbers to be fixed. Instead, we want it to take two input numbers from us and add them.

So what do we have to do for that? We will have to write a program that can take two numbers from us before it runs. So in other words, we will pass the two input values to the program as we call it to run. Now these values that we pass along with the program are called command line arguments!

Alright then! Now that we know what command line arguments are, let us learn how to read these command line arguments into our Python code.

How To Read Command Line Arguments In Python

So as we said in the earlier, reading command line arguments in Python is easy. Because we have a module called sys to do just that!

So using sys module, we can read the two input numbers from the command line. Here is the code for doing just that!

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

Okay. Before I explain what is happening here, let us see how to run this code. To run it, first we save this code in a file called sum.py. So if we want to add two numbers 1 & 2, we will run this program by passing these values as shown:

python sum.py 1 2

And when we run it, we get the following output:

3

So as you can see, the code has taken our input values 1 & 2, added them and then finally printed the result. It is doing exactly what we wanted. Right?

Understanding sys.argv

So now let me explain what is going on in our code:

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

In the first line, we are just importing the sys module. So nothing big going on there. Right? But what is happening in line number 2 & 3?

Let us break it down a little bit. First let us see what is present inside the parenthesis. So we have the code that looks like this:

sys.argv[1]

Now what is going on here? Well, we are making use of the sys module’s argv list. This is the list which will hold our input parameter values 1 & 2. But then why are we indexing it at 1? Well because index 0 will be pointing to the program name sum.py!

So we take the input values 1 & 2 from argv list and type cast it to integers. But why? Because every element of argv list will be a string. So we need to type cast it as required ourselves. Got it?

So from there on, the rest of the code is pretty straight forward. We will just add the two numbers and print the result!

So there you go! That is how we can read command line arguments in our Python code. Now if you have any questions about it, do let me know in the comments below. I will be more than happy to help!

So until next time, take care! 🙂

Want to learn how to check the file size in Python? Check this out!

Categories
COMPUTER SCIENCE DATA MINING DATA SCIENCE DATA VISUALIZATION PROGRAMMING PYTHON TUTORIALS

How To Get Current Timestamp In Python

Are you working on a Python program and now want to get the current timestamp in to your Python code? Then do not worry. Because we will now learn how to do just that!

Sounds good? Great! So let us start then!

Getting Started

Python comes with a variety of libraries to work with time. But among them, two of the libraries stand out for getting the current timestamp. These are the datetime library and the time library.

Get Current Timestamp In Python
Get Current Timestamp In Python

So we can use either of these Python libraries to get the current timestamp. Let us now learn how to use each of them. Alright?

Get Current Timestamp In Python Using Datetime Library

We can use Python’s datetime library to get the current timestamp. To do that, we will need to use it’s now() function. So the example code using datetime library to get the current timestamp looks something like this:

from datetime import datetime
ts = datetime.now().strftime("%B %d, %Y")
print("Current timestamp is: " + ts)

Here, the function datetime.now( ) will return the current timestamp. But the problem is, it is not in human readable format but rather in datetime object format. So we need to convert it from the datetime object to a human readable format. So to do that, we use another datetime function called strftime.

strftime – This function will convert the timestamp from datetime object to a human readable string.

So if you run the code above, you get an output that looks something like this:

'August 17, 2020'

As you can see, we now have our timestamp that is easy to read!

While this is one way for us to get our timestamp, we can use another library to do the same. And that is to use the time library. But how do we use it? Let us take a look at it in the next section.

Get Current Timestamp In Python Using Time Library

Just like the datetime library we saw earlier, we can use the time library to get our current timestamp. But how? To get current timestamp from the time library, we just need to call it’s time function.

Aah…not sure how to do that? Well, take a look at the code below:

import time
secondsFromEpoch = time.time()
print(secondsFromEpoch)

So what output do you get when you run the above code? Well, take a look at it for yourself:

1597651340.6727788

Wait a second! What the hell is going on over here? What are these numbers? They do not look like a timestamp! Right?

Well, you are right in a way. It indeed does not look like a timestamp we normally recognize. But what if I told you that it’s value is actually correct?

Wait what? How is it right? Well, the reason is because it is showing the total number of seconds that has passed since the epoch time.

Wait a minute! Now what is epoch time?

What Is Epoch Time?

Epoch time is the number of seconds that have passed since the midnight of January 1, 1970 GMT.

So when we print the value of secondsFromEpoch, the value that we see is the number of seconds passed since the Epoch Time!

Okay, all that is well and good. But how do we make it more readable?

Well to make it more readable, we will use another helper function from the time module called the localtime( ) function.

So using this function, we can change the above code to print a more readable timestamp value. It looks like this:

import time
secondsFromEpoch = time.time()
timeObj = time.localtime(secondsFromEpoch)
print("The current time is: %d/%d/%d %d:%d:%d" % (
timeObj.tm_mday, timeObj.tm_mon, timeObj.tm_year, timeObj.tm_hour, timeObj.tm_min, timeObj.tm_sec))

And this will give us the output:

The current time is: 17/8/2020 13:32:20

Now, this is more readable. Right?

So there you have it! This is how we can get the current timestamp in Python using the time module.

But are these the only two ways to get a timestamp? Well, no. As I said there are many libraries in Python we can use to get the current timestamp. But these are the two libraries that are easier to use.

Another library that comes to my mind is the calendar module. But this library works along with the time module. So what it does is, it uses the time tuple value given by time module’s gmtime( ) function.

It then converts this value to current time. Now the code looks something like this:

import calendar
import time

ts = calendar.timegm(time.gmtime())
print(ts)

So when you run this code, the output looks like this:

1597656083

So there you have it. These are some of the ways we can get our current timestamp. But if you have any doubts around it, do let me know in the comments below. Alright? So until next time, take care!

Want to learn how to extract data from a website using Python? Check this article!

Categories
COMPUTER SCIENCE TUTORIALS

Which of the following is not a social networking website?

Which one of the following is not a social networking website? (a) Twitter (b)Facebook (c)My Space (d)Hotmail​

So in this article we will try to find out which one of those websites are not a social networking site.

Which of these are not social media sites?
Which of these are not social media sites?

But before we do that, we first need to understand what a social media website is. Right? So let us start from that point.

What Is A Social Media Website?

So there are many ways to answer this question. One is to explain it using take a text book definition of it which says:

Any website that allows people to create & share content to allow them for social networking.

But the problem is, it is not so clear in what it really means. Correct? So we can’t just go by it. Then what can we do about it? Well, let me explain you in a much simpler way. Alright? So here is my way of explaining it.

Any website using which we can create content & post it for the world to see while having a way to talk to others in real time is a social networking website.

What is not a social networking website?

It is not a social networking website if you cannot make your own post public and talk to them at the same time.

Does that make sense?

Answer With Explanation

Well then with that in our mind, let us look at each of the options given in the question. Alright? Here we go!

  1. Twitter – Is this a social networking website? So answer the questions asked above to find out. (i) Can you post on Twitter? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So as you can see, the answers for both the questions is Yes. So that means it is indeed a social networking website!
  2. Facebook – Is this a social networking website? So answer the questions asked above to find out. (i) Can you post publicly on Facebook? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So then this too should a social network!
  3. Myspace – Again, ask the same questions. (i) Can you post publicly on Myspace? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So that means it is indeed a social networking website!
  4. Hotmail – (i) Can you post publicly on Myspace? Answer: No. (ii) Can you talk to others in real time? Answer: Yes. So as you can see here, we cannot post publicly on Hotmail. Which means it is not one!

So from this analysis, we can see that the answer we should be picking up amount the given choices is (d)Hotmail.

What we can learn from here is that if we try to break down the problem into smaller parts, it is easier to answer. Correct? So as you can see, we went through each of the options in the question and tried to answer them. It is only then that we were able to answer it. Right?