I'm tryin to script an annoying task that involves fetching, handling and printing loads of scanned docs - jpeg or pdf. I don't succeed in accessing the printer from python or from windows shell (which I could script with python subproccess module). I succeeded in printing a text file from the command line with lpr command, but not jpg or pdf.
be glad for any clues about that, including a more extensive win shell reference for printing to printer, a suitable python library I missed in my google search stackoverflow search etc (just one unanswered question)
Well, after a little research I found some links that might help you:
1) To print images using Python Shell, this link below has some code using PIL that will, hopefully, do what you want:
http://timgolden.me.uk/python/win32_how_do_i/print.html
2) To print PDF files, this link may have what you need:
http://www.darkcoding.net/software/printing-word-and-pdf-files-from-python/
I never did any of those things, but with a quick look, I could find this links and they seem to make very much sense. Hope it helps :)
I used this for a rtf (just an idea) :
subprocess.call(['loffice', '-pt', 'LaserJet', file])
I am using LibreOffice. it can print in a batch mode.
with a default pdf viewer assigned to the system you can do
import win32api
fname="C:\\somePDF.pdf"
win32api.ShellExecute(0, "print", fname, None, ".", 0)
note that this will only work on windows and will not work with all pdf viewers but it should be good with acrobat and Foxit and several other major ones.
Related
I have searched some method to convert my local html files to pdf file in python.
What I searched was pdfkit, weasyprint, xhtml2pdf, pdfcrowd.
The problem is, I should make this as an exe file with Pyinstaller to give someone else so that they can work with this program. And I encountered an OSerror with pdfkit which is saying:
OSError: No wkhtmltopdf executable found: "b''"
And I found the solution that I have to edit some environment variables for that.
Weasyprint also need to download/install other things.
I don't think those will work in other PC since they need some external treatment to run the program.
xhtml2pdf seems be the one which convert html from the webpage(not the local file) to pdf, and pdfcrowd is my least option for now since I have to pay to use the API.
Do you have any recommendations for converting those with my circumstances?
Do you actually need to provide a python package thing? Because frankly any modern browser should be able to print to a PDF, and that's significantly simpler than anything else if it suffices: have "someone" open the HTML page in their browser, print it, and select whatever option prints to PDF (save as PDF in chrome, print to file in firefox, ...)
First of all, I am new to coding, and I have looked for similar questions and I know some commands which can be used in Python and in CMD to open a file using an executable. The problem I'm having is that when I try to use those commands, the program runs without any error, but it doesn't give the output file that should give. On the other hand, when I just double click the file, which is set to open with *.exe, it works and produces the output.
I tried the CMD command:
start "path of .exe" "filepath"
also just:
"path of .exe" "filepath"
Then I tried the os and subprocess modules in python:
subprocess.Popen(...)
os.system(...)
subprocess.run(...)
and many other solution I found on the internet. The point is that all of these solution don't give errors and should work, but they don't produce the wanted file for me. I used the same commands on another file with a different .exe and they work.
This is the step that is not working in my attempt to automate a whole process. If someone is willing to take a look at the files, you can download them from the following link:
https://gofile.io/?c=5TJtS9
The files are as follows:
1. running the rdam.grd file with hist_dam2d.exe produces the hist.plotps file
2. then running the hist.plotps with plotps.exe produces the wanted diagram
It should be an easy task that doesn't work for me.
For more information... This is part a random finite element software which is freely distributed by the authors. You can see the whole documentation and download all parts of the program from this link:
http://random.engmath.dal.ca/rfem/
The parts that are causing the problem are used just for extracting and showing results.
It is an old software so maybe there is some problem with that.
I don't know what exactly you did with your Python code since you didn't provide the exact code snippets. I also don't have the reputation to comment. So I'll just provide code examples for the 3 methods you listed (CMD, Powershell, Python). All three methods worked on my machine.
1) CMD
start "" "plotps.exe" hist.plotps
The double quotes after the start keyword are there to specify an optional title. What went wrong in your CMD example is that windows thought "plotps.exe" was the title. You don't need to specify a title, but you need to write the quotes. More info on this keyword can be found here: https://ss64.com/nt/start.html
Also note that start is asynchronous
A synchronous way of doing it would be:
plotps.exe hist.plotps
2) Powershell
Start-Process -FilePath "plotps.exe" -ArgumentList "hist.plotps"
I would strongly recommend using powershell over CMD if you have access to both.
This method is synchronous.
More info on Start-Process: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.0
3) Python
I'm not a python expert but this worked for me:
import subprocess
subprocess.call(['plotps.exe', 'hist.plotps'])
I know people already asked how to print Markdown files.
My question is different.
I'm learning Python (The Hard Way), and in ex37 I need to print out some Python codes so I can read them on paper and read them on dead tree, while taking note with a pen and such.
I've been searching Google and S.O and haven't found how to export a .py file, with syntaxic highlighting, into a .pdf file so I may print it out.
Thanks in advance.
py2pdf should do what you need: http://python.net/~gherman/py2pdf.html
I've written a Python script that takes input files and outputs a CSV file. Next, I go into Excel and use conditional formatting to color some of the information (e.g. if A2=1, color background red) -- to keep the formatting I save it as *.xlsx (not sure this is a good idea, currently only tested in windows). This needs to be performed multiple times.
In short: I'd like to incorporate the second stage into the script and this needs to work both in windows and linux.
My thoughts: record an excel macro, have python open csv files and run that macro, then (save as...?) close file or keep it open (optional with command flag -k, --keep_open).
My search came up inconclusive looking for a solution applicable for both OS environments.
This looked close, but it seems windows-centric.
Thoughts/suggestions would be appreciated.
You would almost certainly be better off adding your formatting and doing other operations in python then saving as an Excel xml file.
Since AFAIK Excel is only available for Windows and Mac you are going to be out of luck trying anything like this on Linux. Why not do something similar for Open Office or Libra Office? There is also pySpread which will really whet your appetite.
Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer?
Assuming CPython here, not something clever like using Jython and the Java printing API.
This has only been tested on Windows:
You can do the following:
import os
os.startfile("C:/Users/TestFile.txt", "print")
This will start the file, in its default opener, with the verb 'print', which will print to your default printer.Only requires the os module which comes with the standard library
Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print.
You need to detect the OS your program is running on, then:
For Linux -
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)
For Windows: http://timgolden.me.uk/python/win32_how_do_i/print.html
More resources:
Print PDF document with python's win32print module?
How do I print to the OS's default printer in Python 3 (cross platform)?
To print to any printer on the network you can send a PJL/PCL print job directly to a network printer on port 9100.
Please have a look at the below link that should give a good start:
http://frank.zinepal.com/printing-directly-to-a-network-printer
Also, If there is a way to call Windows cmd you can use FTP put to print your page on 9100. Below link should give you details, I have used this method for HP printers but I believe it will work for other printers.
http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165
You can try wx library. It's a cross platform UI library. Here you can find the printing tutorial:
https://web.archive.org/web/20160619163747/http://wiki.wxpython.org/Printing
I find this to be the superior solution, at least when dealing with web applications. The idea is this: convert the HTML page to a PDF document and send that to a printer via gsprint.
Even though gsprint is no longer in development, it works really, really well. You can choose the printer and the page orientation and size among several other options.
I convert the web page to PDF using Puppeteer, Chrome's headless browser. But you need to pass in the session cookie to maintain credentials.