保存/恢复打印机 DevModes - wxPython / win32print
到目前为止,我已经找到了两种不同的方法来从 wxPython 用户界面访问我认为是等效版本的打印机 DevMode:
window = wx.GetTopLevelWindows()[0].GetHandle()
name = self.itemMap['device'].GetValue() # returns a valid printer name.
handle = win32print.OpenPrinter(name)
dmin = None
dmout = pywintypes.DEVMODEType()
mode = DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT
res = win32print.DocumentProperties(window, handle, name, dmout, dmin, mode)
if res == 1:
print dmout.DriverData
并且
dlg = wx.PrintDialog(self, dgData)
res = dlg.ShowModal()
if res == wx.ID_OK:
print dlg.GetPrintDialogData().PrintData.GetPrivData()
这些二进制结构似乎包含控制设备输出行为的必要信息。这很好,只是它不能直接用于使用存储的 devmode 数据重新加载 PrintSetup 对话框。在第一种情况下,PyDEVMODE 对象包含数十个需要手动设置的单独属性(PyDEVMODE 参考)。在第二种情况下,有一些 Getter / Setter 方法控制某些属性,但不是所有属性(wxPrintData 参考)。有谁知道从实际的 DevMode (二进制数据)创建 Python Devmode 对象的方法(我将采用任一方法,差异很小)?我想避免必须手动存储/重置每个单独的属性,以便对话框每次都以正确的状态重新打开。
So far I've found two different ways to access what I believe are equivalent versions of the Printer DevMode from a wxPython User Interface:
window = wx.GetTopLevelWindows()[0].GetHandle()
name = self.itemMap['device'].GetValue() # returns a valid printer name.
handle = win32print.OpenPrinter(name)
dmin = None
dmout = pywintypes.DEVMODEType()
mode = DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT
res = win32print.DocumentProperties(window, handle, name, dmout, dmin, mode)
if res == 1:
print dmout.DriverData
and also
dlg = wx.PrintDialog(self, dgData)
res = dlg.ShowModal()
if res == wx.ID_OK:
print dlg.GetPrintDialogData().PrintData.GetPrivData()
These to binary structures appear to contain the necessary information to control the device output behavior. This is fine and well, except that it can't be directly used to reload the PrintSetup dialogs with this stored devmode data. In the first case the PyDEVMODE object contains dozens of individual properties that need to be manually set (PyDEVMODE Reference). In the second case there are a handful of Getter / Setter methods that control some of the properties, but not all of them (wxPrintData Reference). Is anyone aware of a way to create a Python Devmode Object (I'll take either approach, the differences are trivial) from the actual DevMode (the binary data)? I'd like to avoid having to manually store / reset each individual attribute in order for the dialogs to re-open in the correct state every time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以在
win32print.GetPrinter()
中编辑pDevMode
对象,方法是通过新的命名对象进行操作。我在类似的问题中扩展了 Yuri Gendelman 提供的“属性”命名结构: 通过 Python 以双面模式打印 PDF 文件
这是我在代码中如何使用它的示例。
以下是检查默认设置的代码:
win32print.GetPrinter(handle, level)['pDevMode'].Copies
win32print.GetPrinter(handle, level)['pDevMode'].Duplex
以下是您可以使用
pDevMode
更改的其他打印机设置:http://timgolden.me.uk/pywin32-docs/PyDEVMODE.htmlYou can also edit the
pDevMode
object withinwin32print.GetPrinter()
by manipulating it through a new named object.I expanded upon the "attributes" naming structure provided by Yuri Gendelman in a similar question: Print PDF file in duplex mode via Python
Here is a sample of how I used it in my code.
Here is the code to check default settings:
win32print.GetPrinter(handle, level)['pDevMode'].Copies
win32print.GetPrinter(handle, level)['pDevMode'].Duplex
Here are the other printer settings you can change with
pDevMode
: http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html看来目前还没有优雅的方法可以直接在 Python 中实现这一点。我能想到的最接近的是一个用 c++ 编写的 dll,我可以调用它。我最终得到了完整的二进制 DevMode 结构,并且可以从中重新加载。该 c++ dll 的代码如下所示:
在 Python 中,它的调用方式如下:
It appears that at this point there's no elegant way to achieve this directly in Python. The closest I was able to come up with was a dll written in c++ that I'm able to call into. I end up with the full binary DevMode Structure, and can reload from it. The code for that c++ dll looks like this:
In Python, it's called into like so: