windows下如何用python控制打印机打印_如何在Windows中使用python脚本直接打印而不显示打印对话框? - python...

我已经创建了一个桌面应用程序,可以在Windows OS中使用python2.7和gtk + 3从打印机打印令牌。我的应用程序中的按钮应从文件中调用打印。为了格式化打印件,我使用了.rtf文件,请先打开相应的文本编辑器(在我的情况下为MS Word),然后再将文件从打印机中打印出来,然后立即关闭。

如何避免打印之前打开和关闭它?无论是MS Word设置,Windows还是Python解决方案。

这是我的代码:

def make_print(self):

os.startfile("print.rtf", "print")

注意“ print.rtf”是在此调用之前由python脚本创建的。

我也尝试过,但是它甚至都没有打印。

def make_print1(self):

with open('print.rtf', 'r') as f, open('LPT1:', 'w') as lpt:

while True:

buf = f.read()

if not buf: break

lpt.write(buf)

python大神给出的解决方案

此解决方案仅在Windows中有效。为此,您需要安装pywin32 [http://timgolden.me.uk/pywin32-docs/contents.html]模块。

除了创建rtf或ps,我们还可以使用DC(设备上下文)将其直接发送到打印机。

这是我尝试过的解决方案。

import win32print, win32ui, win32gui

import win32con, pywintypes

# create a dc (Device Context) object (actually a PyCDC)

dc = win32ui.CreateDC()

# convert the dc into a "printer dc"

# get default printer

printername = win32print.GetDefaultPrinter ()

# leave out the printername to get the default printer automatically

dc.CreatePrinterDC(printername)

# you need to set the map mode mainly so you know how

# to scale your output. I do everything in points, so setting

# the map mode as "twips" works for me.

dc.SetMapMode(win32con.MM_TWIPS) # 1440 per inch

# here's that scaling I mentioned:

scale_factor = 20 # i.e. 20 twips to the point

# start the document. the description variable is a string

# which will appear in the print queue to identify the job.

dc.StartDoc('Win32print test')

# to draw anything (other than text) you need a pen.

# the variables are pen style, pen width and pen color.

pen = win32ui.CreatePen(0, int(scale_factor), 0)

# SelectObject is used to apply a pen or font object to a dc.

dc.SelectObject(pen)

# how about a font? Lucida Console 10 point.

# I'm unsure how to tell if this failed.

font = win32ui.CreateFont({

"name": "Lucida Console",

"height": int(scale_factor * 10),

"weight": 400,

})

# again with the SelectObject call.

dc.SelectObject(font)

# okay, now let's print something.

# TextOut takes x, y, and text values.

# the map mode determines whether y increases in an

# upward or downward direction; in MM_TWIPS mode, it

# advances up, so negative numbers are required to

# go down the page. If anyone knows why this is a

# "good idea" please email me; as far as I'm concerned

# it's garbage.

dc.TextOut(scale_factor * 72,

-1 * scale_factor * 72,

"Testing...")

# for completeness, I'll draw a line.

# from x = 1", y = 1"

dc.MoveTo((scale_factor * 72, scale_factor * -72))

# to x = 6", y = 3"

dc.LineTo((scale_factor * 6 * 72, scale_factor * 3 * -72))

# must not forget to tell Windows we're done.

dc.EndDoc()

在Windows8.1 / Python 3.4上测试

参考:http://newcenturycomputers.net/projects/pythonicwindowsprinting.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值