python win32print不打印 [英] python win32print not printing

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

问题描述

我需要直接打印一些信息(无需用户确认),并且我正在使用 Python 和 win32print 模块.

I need to print some information directly (without user confirmation) and I'm using Python and the win32print module.

我已经阅读了整个 Tim Golden win32print 页面(甚至阅读win32print doc,它很小),我正在使用相同的示例自己在那里写的,但我什么也没打印.

I've already read the whole Tim Golden win32print page (even read the win32print doc, which is small) and I'm using the same example he wrote there himself, but I just print nothing.

如果我进入交互式 shell 并一次执行一个步骤,我会在打印机队列中获取文档(在 StartDocPrinter 之后),然后我获取文档大小(在 StartDocPrinter 之后)>StartPagePrinter、WritePrinter、EndPagePrinter 块),然后文档从队列中消失(在 EndDocPrinter 之后)而不打印.

If I go to the interactive shell and make one step at a time, I get the document on the printer queue (after the StartDocPrinter), then I get the document size (after the StartPagePrinter, WritePrinter, EndPagePrinter block) and then the document disappear from the queue (after the EndDocPrinter) without printing.

我知道 Tim Golden 展示的 ShellExecute 方法.它在这里工作,但它需要创建一个临时文件并打印此文件名,这是我不想要的两件事.

I'm aware of the ShellExecute method Tim Golden showed. It works here, but it needs to create a temp file and it prints this filename, two things I don't want.

有什么想法吗?提前致谢.

Any ideas? Thanks in advance.

这是我正在测试的代码(复制并粘贴 Tim Golden 的代码):

This is the code I'm testing (copy and paste of Tim Golden's):

import os, sys  
import win32print
import time
printer_name = win32print.GetDefaultPrinter()
if sys.version_info >= (3,):
    raw_data = bytes ("This is a test", "utf-8")
else:
    raw_data = "This is a test"

hPrinter = win32print.OpenPrinter (printer_name)
try:
    hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW"))
    try:
        win32print.StartPagePrinter (hPrinter)
        win32print.WritePrinter (hPrinter, raw_data)
        win32print.EndPagePrinter (hPrinter)
    finally:
        win32print.EndDocPrinter (hPrinter)
finally:
    win32print.ClosePrinter (hPrinter)

我在我的电脑上安装了一台 pdf 打印机,用另一台打印机(CutePDF Writer)进行测试,我可以生成 test of raw data.pdf 文件,但是当我查看内部时,什么也没有.含义:除了 WritePrinter 之外的所有命令似乎都在做他们应该做的事情.但是,正如我在评论中所说,WritePrinter 返回应该写入打印机的正确字节数.我不知道如何解决这个问题,但我认为我的打印机没有问题.

I installed a pdf printer in my computer to test with another printer (CutePDF Writer) and I could generate the test of raw data.pdf file, but when I look inside there is nothing. Meaning: all commands except WritePrinter appears to be doing what they were supposed to do. But again, as I said in the comments, WritePrinter return the correct amount of bytes that were supposed to be written to the printer. I have no other idea how to solve this, but just comproved there is nothing wrong with my printer.

推荐答案

我仍在寻找解决此问题的最佳方法,但我找到了一个让自己满意的答案.在 Tim Golden 的网站(有问题的链接)中,您可以找到以下示例:

I'm still looking for the best way to do this, but I found an answer that satisfy myself for the problem that I have. In Tim Golden's site (linked in question) you can find this example:

import win32ui
import win32print
import win32con

INCH = 1440

hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (win32print.GetDefaultPrinter ())
hDC.StartDoc ("Test doc")
hDC.StartPage ()
hDC.SetMapMode (win32con.MM_TWIPS)
hDC.DrawText ("TEST", (0, INCH * -1, INCH * 8, INCH * -2), win32con.DT_CENTER)
hDC.EndPage ()
hDC.EndDoc ()

在阅读了大量文档后,我对其进行了一些修改.我将使用 win32ui 库和 TextOut(设备上下文方法对象).

I adapted it a little bit after reading a lot of the documentation. I'll be using win32ui library and TextOut (device context method object).

import win32ui
# X from the left margin, Y from top margin
# both in pixels
X=50; Y=50
multi_line_string = input_string.split()
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (your_printer_name)
hDC.StartDoc (the_name_will_appear_on_printer_spool)
hDC.StartPage ()
for line in multi_line_string:
     hDC.TextOut(X,Y,line)
     Y += 100
hDC.EndPage ()
hDC.EndDoc ()

在回答我自己的问题和 此处 我发现这是一种鼓励行为,因此我正在这样做.我再等一会儿看看有没有其他答案.

I searched in meta stackoverflow before answering my own question and here I found it is an encouraged behavior, therefore I'm doing it. I'll wait a little more to see if I get any other answer.

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

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