0

Is there a way to iterate through and print multiple urls with python? And by print i don't mean print() but send to a printer to be printed. I have the following, but want to essentially right click > print each of the pages. Is this possible?

Thanks in advance.

On Windows if thats relevant.

nums = [2015, 2017, 2019, 2021]
url_link = 'www.website.com/{}'

for x in nums:
    url = url_link.format(x)

 
MB2000
  • 13
  • 3

2 Answers2

1

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?

Python: What OS am I running on?

Emin
  • 49
  • 10
0

It is not possible to say you should not be able to, since there are means, however consider

  • Native raw Windows needs a suitable format handler, so basic print() will only print using a text handler and there are better ways using NotePad or WordPad scripting.
  • For URLs the handler is MS Edge and that can --headless print URLs to PDF but not what your after.

enter image description here

@echo off & Title Headless PDF Print
SETLOCAL ENABLEDELAYEDEXPANSION

Rem clear any old run variable
set "Output="

Rem the prefered Chromium version/Profile
set "Chrome=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"


Rem here I first set output folder to same as batch file
set "Output=%~dp0Out.pdf"

Rem override with valid given.pdf
if /i %~x1. == .pdf. (set "Output=%~1")

echo Printing PDF to !Output!

"%Chrome%" --enable-logging --headless --disable-gpu --print-to-pdf-no-header --run-all-compositor-stages-before-draw --print-to-pdf="!Output!" --disable-extensions %2

Thus using raw Windows

The only all-round working basic solution (without 3rd party tools) is to invoke selection of the printer as default printer, which can be done using Basic Shell (or cmd) scripting, then open the URL with Edge, then script the print trigger (Ctrl+P) as shown bottom right of image.

You can use Python to emulate those steps but easier if done from the cmd line or using a library that does url2prn based perhaps on installing non commercial or licensed Ghostscript.

K J
  • 8,045
  • 3
  • 14
  • 36