I tried to find a way to print a PDF using Python on Windows.
This time, we will call an external application to print.
Postscript: 2015-07-03: The sample code can be done with
python [filename]
. Some are different, and no exceptions are included. Please check the code for details.
Command line options for Acrobat and Adobe Reader
Adobe Reader and Adobe Acrobat have command line options. You can start it in another process (instance) or run printing as it is.
The example here uses Adobe Acrobat. (ʻAcrobat.exe) Adobe Reader uses ʻAcroRd32.exe
. The installation path also depends on the version. Is the disadvantage that the judgment is troublesome?
Reference: Print PDFs with Python? | GeoNet
https://gist.github.com/hrsano645/ddd5c111204338576404
# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
import subprocess
import sys
# ref: https://geonet.esri.com/thread/59446
# ref: https://helpx.adobe.com/jp/acrobat/kb/510705.html
def main(pdffile):
# acroread = r'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
acrobat = r'C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe'
# '"%s"'is to wrap double quotes around paths
# as subprocess will use list2cmdline internally if we pass it a list
# which escapes double quotes and Adobe Reader doesn't like that
cmd = '"{}" /P "{}"'.format(acrobat, pdffile)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
exit_code = proc.wait()
if __name__ == '__main__':
pdffile = sys.argv[1]
main(pdffile)
This method requires the printer name. (I have not checked if there is none. Please comment somebody)
https://gist.github.com/hrsano645/a16c3f35deac04b2b4d9
# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
import subprocess
import sys
# ref: https://geonet.esri.com/thread/59446
# ref: https://helpx.adobe.com/jp/acrobat/kb/510705.html
def main(pdffile, printer_name):
# acroread = r'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
acrobat = r'C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe'
# '"%s"'is to wrap double quotes around paths
# as subprocess will use list2cmdline internally if we pass it a list
# which escapes double quotes and Adobe Reader doesn't like that
cmd = '"{}" /N /T "{}" "{}"'.format(acrobat, pdffile, printer_name)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
exit_code = proc.wait()
if __name__ == '__main__':
pdffile = sys.argv[1]
printer_name = sys.argv[2]
main(pdffile, printer_name)
Use ShellExecute in the Windows API available on Pywin32.
With ShellExecute, you can use open to open in the associated application and print to print.
It seems to work with this script, but in my environment there were times when I could print and times when I couldn't.
reference:
https://gist.github.com/hrsano645/b69b071de4b8cb59cbe9
# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
# ref: http://timgolden.me.uk/python/win32_how_do_i/print.html
# ref: http://docs.activestate.com/activepython/2.7/pywin32/win32api__ShellExecute_meth.html
import win32api
import sys
if __name__ == '__main__':
pdf_file_name = sys.argv[1]
win32api.ShellExecute(0, "print", pdf_file_name, None, ".", 0)
Print using a command line application called GSPrint (included with GSView) that uses Ghostscript.
First, install Ghostscript and GSView. (The one used at the time of verification is the original version, but I think the Japanese version is probably the same)
Ghostscript 9.16 and GSview 5.0 J (Official Site)
After that, use the subprocess module to execute the command.
If you do not specify the -query option, it will be printed as is.
https://gist.github.com/hrsano645/fe73570d0d5d8df4863a
# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
import subprocess
import sys
# ref: http://stackoverflow.com/questions/4498099/silent-printing-of-a-pdf-in-python
# ref: http://stackoverflow.com/questions/1462842/print-pdf-document-with-pythons-win32print-module
def main(pdffile):
gsprint = r"C:\Program Files\Ghostgum\gsview\gsprint.exe"
# -Call the printer list with the quert option
cmd = '"{}" -query "{}"'.format(gsprint, pdffile)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
exit_code = proc.wait()
if __name__ == '__main__':
pdffile = sys.argv[1]
main(pdffile)
As an important notice, since the paper size setting at the time of printing cannot be operated, it seems that there is no choice but to set the enlargement / reduction on the printer side after displaying the print dialog with -query. However, the PX-1700F I am using does not work for some reason even if I enlarge it. Is this a problem on the printer side?
(I tried it later, but if the paper size of the printer and the paper size set in the file match, it prints beautifully.)
At the time of printing, the color was monochrome, so it may be necessary to specify it in advance on the GSPrint side. (I haven't verified it in detail, but as far as I can see the options, I wonder if it needs to be set)
reference:
There seems to be another way to do this using the GUI toolkit available in Python. I found something like that in wxpython and pyqt (pyside), but I haven't verified it yet. If I can verify it, I will write the continuation.
In addition, if you know a good printing method, I would appreciate it if you could write it in the comments.
Recommended Posts