Python Forum
Can you print a string variable to printer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you print a string variable to printer
#1
I can not send a string variable to the printer because it keeps looking for a file. I have printed the string variable out on the terminal window and it is formatted and looks the way i want it but need a hard copy.

I am using the variable to temporarily store lines of text with new line '\n':
self.txt_print_var=self.txt_print_var+t+'\n'

To try and print:
os.system('lpr self.txt_print_var')

lpr: Error - unable to access "self.txt_print_var" - No such file or directory

Is there a way to print variable or am I going to have to create a file to get it to work
Reply
#2
Don't use os.system anymore it has been replaced bye subprocess.
To print a variable can eg use Popen.stdin
Something like untested.
import subprocess

var = 'Test print'
lpr = subprocess.Popen(["lpr"], stdin=subprocess.PIPE)
lpr.stdin.write(var)
With run.
import subprocess
from io import StringIO

var = 'Test print'
var_file = StringIO(var)
lpr = subprocess.run(["lpr"], var_file)
Reply
#3
Thank you.
Problem resolved
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 728 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Print variable without '' and spaces arnonim 1 694 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 1,883 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Problem with print variable in print.cell (fpdf) muconi 0 632 Dec-25-2022, 02:24 PM
Last Post: muconi
  Trying to send file to printer with no results. chob_thomas 2 3,258 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  python r string for variable mg24 3 2,634 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 918 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,123 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,705 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85

Forum Jump:

User Panel Messages

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