python - 如何在 Windows 中使用 python 脚本直接打印而不显示打印对话框?

标签 python windows printing

我创建了一个桌面应用程序来从打印机打印 token ,在 Windows 操作系统中使用 python2.7gtk+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)

最佳答案

此解决方案仅适用于 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

关于python - 如何在 Windows 中使用 python 脚本直接打印而不显示打印对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329924/

相关文章:

Python - 全局名称未定义

python - 如何在 python igraph 中从 .gml 文件创建加权图

windows - PE文件中section和数据目录是什么关系?

javascript - CSS 打印媒体查询删除空格

jquery - 打印可滚动表格的全部内容

python - pylint 提示 py.test : "Module ' pytest' has no 'raises' member"

python - 使用 Datanitro 的交互式工作表

在 CPP 文件 (Visual Studio) 中实现的 C++ 模板错误

c# - DLL 中的 CreateWindowEx() 创建一个标题为奇数的窗口

java - PDFBox: 打印后立即显示 "you did not close a PDFDocument"