使用 python 发送要在打印机上打印的 URL

发布于 2025-01-20 07:21:54 字数 275 浏览 0 评论 0原文

有没有办法用 python 迭代并打印多个 url?我所说的打印并不是指 print() ,而是发送到打印机进行打印。我有以下内容,但本质上想右键单击>打印每一页。这可能吗?

提前致谢。

在 Windows 上(如果相关的话)。

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

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

 

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)

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2025-01-27 07:21:54

不幸的是,没有在所有平台上使用 Python 进行打印的标准方法。因此,您需要编写自己的包装函数来打印。

您需要检测您的程序运行的操作系统,然后:

对于 Linux -

 import subprocess
lpr =  subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)

对于 Windows:http://timgolden.me.uk/python/win32_how_do_i/print.html

更多资源:

使用python的win32print模块打印PDF文档?

Python:我在什么操作系统上运行?

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?

知你几分 2025-01-27 07:21:54

不可能说您不应该这样做

  • 由于有手段,因此 脚本。
  • 对于URL,处理程序是MS Edge,它可以 - 无头打印URL到PDF,但不是您之后的内容。

@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 --no-pdf-header-footer --run-all-compositor-stages-before-draw --print-to-pdf="!Output!" --disable-extensions %2

因此,使用RAW Windows

唯一的全面工作基本解决方案(没有第三方工具)是调用打印机的选择为默认打印机,可以使用基本的shell(或cmd)脚本来完成该打印机,然后用边缘打开URL,然后脚本脚本打印触发器(CTRL+P),如图像的底部所示。

您可以使用Python模拟这些步骤,但是如果从CMD线完成或使用基于URL2PRN的库,则可能会基于安装非商业或许可的Ghostscript。

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 --no-pdf-header-footer --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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文