使用Python将打印作业发送到USB打印机
答
这听起来像你正在使用Windows,所以让我们先从在于 - 如果您使用的是Linux,则应答更改。
在Windows中有两种打印方法。第一种最常见的方式是通过Windows GDI接口发送各个绘图命令。要做到这一点,您必须将每个单独的元素放置在适当的位置(文本字符串,图像和形状),同时选择正确的颜色和字体。如果您要自己生成数据,那么很容易,如果您必须解析正在阅读的文件,则要困难得多。
另一种选择是以“原始”模式发送到打印机,打印机驱动程序基本上被旁路。为了达到这个目的,打印机必须本地理解你提供给它的字节流。有一些打印机本身可以理解Postscript,但我不确定PDF,PRN不是标准格式。
我从来没有过的Python做原料印刷自己,但这里给的示例代码很短的片段的链接(和问题的想法,希望):http://bytes.com/topic/python/answers/512143-printing-raw-postscript-data-windows
答
import wx
import win32api
import win32print
class ComboBoxFrame(wx.Frame):
def __init__(self):
# creates a drop down with the list of printers available
wx.Frame.__init__(self, None, -1, 'Printers', size=(350, 300))
panel = wx.Panel(self, -1)
list=[]
#Enum printers returns the list of printers available in the network
printers = win32print.EnumPrinters(
win32print.PRINTER_ENUM_CONNECTIONS
+ win32print.PRINTER_ENUM_LOCAL)
for i in printers:
list.append(i[2])
sampleList = list
wx.StaticText(panel, -1, "Please select one printer from the list of printers to print:", (15, 15))
self.combo =wx.ComboBox(panel, -1, "printers", (15, 40), wx.DefaultSize,sampleList, wx.CB_READONLY)
btn2 = wx.Button(panel, label="Print", pos=(15, 60))
btn2.Bind(wx.EVT_BUTTON, self.Onmsgbox)
self.Centre()
self.Show()
def Onmsgbox(self, event):
filename='duplicate.docx'
# here the user selected printer value will be given as input
#print(win32print.GetDefaultPrinter())
win32api.ShellExecute (
0,
"printto",
filename,
'"%s"' % self.combo.GetValue(),
".",
0
)
print(self.combo.GetValue())
if __name__ =='__main__':
app = wx.App()
ComboBoxFrame().Show()
app.MainLoop()
+0
代码转储(技术上可能是正确的)并不一定帮助OP或未来的访问者。我会通过解释代码来填补答案,即使代码中的注释也很有帮助。 – Bugs 2017-03-06 09:08:14
这的确是一个问题超级用户网站。 – Keith 2011-06-13 02:02:20
你说“USB”就好像它改变了问题。 – 2011-06-13 02:05:40
@Ignacio:也许他认为它做到了。我们都在这里学习。 – mpen 2011-06-13 04:20:29