Python Forum

Full Version: Is this possible in Python? Auto-Send-to-printer on script run?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Is this possible in Python?

I'm completing a project and as a last step for some non-technical coworkers, I want to automatically send to a printers to print when the script is run.

I've got an 11-page .xlsx spreadsheet separated onto 11 different worksheets. Can I use Python to go through all 11 worksheets and send each to our local network printer when the Python script is run? (Cloud printing is not an option = tight work network)

Thanks,
phil

Okay, I seem to have the code to auto-print to the default printer.
import os
os.startfile('C:/Python36x64/testFile.txt', 'print') 
What I'm missing is how to select all worksheets in the Excel-.xlsx file I am printing.
Anyone know how to (ideally use Openpyxl) select all worksheets in a spreadsheet using Python?
If you have libreoffice, you can try to convert an xlxs document to pdf from the command line using the soffice command
Output:
soffice -env:UserInstallation=file:///tmp/test --headless --convert-to pdf:calc_pdf_Export --outdir ./tmp fubar.xlsx
You need to adapt the paths to the directories and the xlsx file to your system (this is a linux command). This command creates a file ./tmp/fubar.pdf. Then you only need to send the pdf file to the printer (there are tools in windows for this such as pdftoprinter or gsprint).

If you can do it from the command line, you can do it from python by using subprocess.call() for example.

Another strategy that I see is to write an ironpython script that can interact with Microsoft Office's API, but I have never done this.
Hi Buran,

I read through those two SO posts before posting here. Thanks though for them. That's how I learned about the OS-print method.

Unfortunately LibreOffice isn't an option. I making this for the lowest common denominator co-worker and I basically need this Python script to be an icon on the desktop, double-click, and print to the default printer. That's the ideal I'm after anyway.

I have the SS complete, 11 worksheets within one SS file.
If I have the SS open, I can click the left-most worksheet, hold the shift key down, and click the right-most SS, thereby selecting all worksheets.
Then by printing (Ctrl-P & Enter), I will print all worksheets in the SS.

It is possible to do this by implementing a coded software solution?

Thanks,
Phil
OS? If you are on Linux you could use cups and subprocess.
We're all on Win10 (some still on 7), mostly 64 bit (mine is 64-haven't checked everyone)

I'm working for a pretty tight and secure company (UHG) so installing third-party, or any other software, is a big event. Getting Python installed on computers isn't difficult as it's already approved in our internal appstore.

FYI: I'm trying to bring some automation and management time-savings into this archaic dep't I'm in, bolster my Python knowledge and practical project completions, and work myself into a full-time developer position that pays well enough to support my family (as I am now). I'm also working nights (CST-USA) over long weekends so I'm in and out of here as the week goes on.

Thanks,
phil

Also, on my own developer laptop, I have it set to dual boot into Win10 or Linux Mint. I can't do this with my co-workers though. Just to let you know I have some experience with Linux.

Phil
You can try something from this list with subprocess module
Hi,

Printing is not my roadblock with Python right now, it's selecting all worksheets.
Quote:I have the SS complete, 11 worksheets within one SS file.
If I have the SS open, I can click the left-most worksheet, hold the shift key down, and click the right-most SS, thereby selecting all worksheets.
Then by printing (Ctrl-P & Enter), I will print all worksheets in the SS.

It is possible to do this by implementing a coded software solution?

Phil
(Nov-06-2018, 06:30 AM)pcsailor Wrote: [ -> ]Printing is not my roadblock with Python right now, it's selecting all worksheets.

If printing is not a roadblock then can't you iterate over sheets and print them one by one (sheetnames)?