Python Forum
Print the file using the associated application on Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the file using the associated application on Linux
#1
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
Reply
#2
man lpr

$ lpr <options> file

So with subprocess is going to be subprocess.call(['lpr', '-one-option', '-second-option', 'file_path/file'])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create exe file for linux? korenron 2 908 Mar-22-2023, 01:42 PM
Last Post: korenron
  Saving the print result in a text file Calli 8 1,700 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 5,946 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,189 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 10,687 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,768 Oct-17-2021, 05:08 AM
Last Post: davidm
  How to send data from a python application to an external application aditya_rajiv 1 2,131 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  Why it does not print(file.read()) Rejaul84 1 2,309 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  get two characters, count and print from a .txt file Pleiades 9 3,291 Oct-05-2020, 09:22 AM
Last Post: perfringo
  Cannot redirect print to a file tester_V 3 2,454 Sep-11-2020, 12:21 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020