Python Forum
Print the file using the associated application on Linux - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print the file using the associated application on Linux (/thread-5648.html)



Print the file using the associated application on Linux - Borisko - Oct-15-2017

Based on this post I want to know whether it is possible to send file to default printer using the associated on Linux?
I've tested this exampe on Windows and it works fine! So, I can create any file (.txt, .docx, .xlxs e.t.c.) and python script automatically opens it with associated application and prints it immediately.

#For Windows
import os
os.startfile("C:/Users/TestFile.txt", "print")
I've already tested other code sample on Linux and it works but not as I would like. The issue is that it prints only string but not the file itself. I looked at several other examples and it seems that this function can send for printing the file content only, am I right?

#For Linux
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write("Some text to print here...")
I want to make it work for Linux as well as it works for Windows. Is it possible? I really don't want to make complex mouse automation just for file printing on Linux. Plz help noob like me Big Grin


RE: Print the file using the associated application on Linux - wavic - Oct-15-2017

man lpr

$ lpr <options> file

So with subprocess is going to be subprocess.call(['lpr', '-one-option', '-second-option', 'file_path/file'])