打印文件并配置打印机设置 [英] Printing a file and configure printer settings

查看:60
本文介绍了打印文件并配置打印机设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows上使用Python编写打印机自动化代码,但无法完成.

I'm trying to code a printer automation using Python on Windows, but can't get it done.

我不太了解这个主题,我有些惊讶-似乎不存在简单"的方法来完成此任务.有这么多的API可以轻松方便地访问普通事物,但是打印似乎是特殊的"..

I'm not really understanding the topic and i'm a little surprised - a "simple" way to get this done doesn't seem to exist..? There are so many APIs that allow to access common things in a nice and easy way, but printing seems to be something "special"..?

 

这是我所拥有的以及我想做的事情:

Here is what i have and what i want to do:

  • 有一个PDF文件.PDF已经存在,我不想创建PDF或任何其他文件类型.我喜欢打印此PDF文件.一次一个文件.

  • There is a PDF file. The PDF already exists, i do not want to create PDFs or any other filetype. I like to print this PDF file. One file at a time.

该文件可以采用横向和纵向布局.该文件可能具有以下大小之一:A4,A3,A2,A1和A0.

The file could be in landscape and portrait layout. The file could have one of this sizes: A4, A3, A2, A1 and A0.

我喜欢使用普通的物理"打印机打印文件.打印机是网络设备,并使用其IP连接.有各种各样的网络打印机,我希望能够使用其中的一种以上.有些只是小型A4打印机,有些是大型办公设备(多合一扫描,复印,打印等),也有大型绘图仪(最大A0尺寸的纸张).

I like to print the file using a normal, "physical" printer. The printer is a network device and connected using its IP. There are various network printers and i'd like to be able to use more than one of them. Some are just small A4-printers, some are big office devices (all-in-one scan, copy, print, ...) - and there are big plotters, too (up to A0 sized paper).

我想编码:在此打印机上打印此PDF文件".

I'd like to code: "Print this PDF file on this printer".

我喜欢配置打印尺寸.我想按原样打印PDF,但我也希望能够在较小的纸张尺寸上打印大格式.像,PDF本身是A0大小,但我想在A3纸上打印.或原始的PDF尺寸是A2,我想将其打印在A4上.

I like to configure the printing size. I'd like to print the PDF "as it is", in its original size - but i want to be able to print big formats on small paper sizes, too. Like, the PDF itself is an A0 size, but i want to print it on A3 paper. Or the original PDF size is A2 and i want to print it on A4.

我想在Windows 7计算机(SP1,64位)上使用它.而且我正在尝试用python编写代码.我使用的是python 2.7,因为我使用的是python 3中不提供的一些第三方模块.通常,欢迎可以通过python脚本触发的所有可行解决方案.

I'd like to use that on Windows 7 computers (SP1, 64bit). And i'm trying to code that in python. I'm using python 2.7, as i'm using some third-party-modules that are not available in python 3. In general, every working solution that can be triggered through a python script is welcome.

 

对我来说,这似乎并不是一件非常复杂"的任务.手动"操作非常简单明了-选择文档,开始打印,选择打印机,选择纸张尺寸-然后打印.

To me, that doesn't seem to be a very "complex" task to do. Doing that "by hand" is very easy and straightforward - select the document, start printing, select the printer, select the paper size - and print.

通过代码执行此操作似乎很困难.这是我到目前为止遇到的东西.

Doing this by code seems to be rather difficult. Here is what i've come across until now.

  • 有许多可用于命令行打印的程序,例如"Acrobat Reader","Foxit Reader"或类似程序.虽然使用这些程序提供的命令可以完美地进行打印,但无法访问打印机设置来配置纸张尺寸.

  • There are various programs that could be used for command line printing, programs like "Acrobat Reader", "Foxit Reader" or similar. While it works perfect to print using the commands those programs provide, it's not possible to access the printer settings to configure the paper size.

有特殊的命令行打印工具,但是我找不到任何有用的免费软件.我已经尝试过"VeryPDF"命令行工具,但是在纸张尺寸方面使用它时遇到了一些问题.虽然对各种字母格式和各种其他内容都提供了完美的支持,但不支持我需要的纸张尺寸(A4至A0).有A4和A3的预设,可以使用.该工具可以选择仅通过测量(in/pt/mm)来使用自定义纸张尺寸-但这并不如示例所示那样有效,使用此方法时,它始终可以打印到A4.

There are special command line printing tools, but i couldn't find any useful freeware. I have tried the "VeryPDF" command line tool, but had some problems using it when it comes to paper sizes. While there is perfect support for a whole range of letter-formats and various other things, the paper sizes i need (A4 to A0) somehow aren't supported. There are presets for A4 and A3, those work. The tool has the option to use custom paper sizes by just passing the measurements (in/pt/mm) - but that didn't work as the examples show, it always prints to A4 when using this method.

我找到了适用于python的win32软件包,包括win32print.我没有这个东西.该API提供了查找,添加或删除打印机,列出打印机队列,启动和停止打印机作业等功能,但没有简单的方法可以打印文件.似乎可以使用此API通过通过python编码创建打印数据来添加打印机作业,将一些文本和/或图形压入文件"之类,然后将其发送给打印机.打印机已经可以理解的格式.

I've found the win32-package for python, including win32print. I don't get this thing. The API provides functions to find, add or remove printers, list a printer queue, start and stop printer jobs and so on - but no simple possibility to print a file. It seems like this API could be used to add a printer job by creating the printing data by python coding, push some text and/or graphics in something like a "file" and send that to the printer. In a format the printer already understands.

 

当使用此win32print模块时,我无法使其正常工作.这是我尝试使用的示例代码片段:

And when using this win32print module, i can't get it to work correctly. Here is an exmple snippet i tried to use:

from win32print import *
printer = GetDefaultPrinterW()
handle = OpenPrinter(printer)
info = GetPrinter(handle, 2)
cap = DeviceCapabilities(info['pPrinterName'], info['pPortName'], DC_PAPERS)
ClosePrinter(handle)

...如此处所述:

http://timgolden.me.uk/pywin32-docs/win32print__DeviceCapabilities_meth.html

但这只是返回:

NameError: name 'DC_PAPERS' is not defined

每当我尝试使用需要传递此类常量的函数时,就会发生这种情况.我的系统上没有定义一个常量,我也不知道为什么.

That happens whenever i'm trying to use a function that needs such constants passed. Not a single constant is defined on my system and i don't know why.

但是,即使正常工作,我也不知道是否可以使用此API,所有使用示例仅说明了如何将文本字符串发送到打印机.那不是我所需要的,那不是我想知道的.

But i don't know if i could use this API even when working correctly, all the usage examples are just showing how to send a text-string to a printer. That's not what i need, that's not what i want to know.

 

是否有任何可行的解决方案来打印文件并以简单明了的方式设置打印大小?

Is there any working solution to print a file and to set the printing size in a simple and straightforward way?

欢迎想法和提示!

推荐答案

查看"Tim Golden网站上的如何打印"页面.当您提出问题时,此页面与2​​014年的页面相同.有一个打印JPG文件的示例,该文件也可以操纵打印机设置.

Look at the "How To Print" page on Tim Golden's website. This page was the same in 2014 when you asked your question. There's an example printing a JPG file that also manipulates the printer settings.

这并不是您正在做的事情的完美示例,但它可以使您走上正确的道路.

That's not quite a perfect example for what you're doing but it should get you on the right track.

在win32con中定义了DC_PAPERS:

DC_PAPERS is defined in win32con:

import win32con
x = win32con.DC_PAPERS

你怎么知道的,我不知道.对于那些已经熟悉Python之外的Win32API的人来说,这也许是显而易见的" ...

How you're supposed to know that, I have no idea. Perhaps it's "obvious" to those already familiar with the Win32API outside of Python...

这篇关于打印文件并配置打印机设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆