win32print和多台打印机

时间:2011-11-22 08:52:07

标签: python windows pdf printing win32com

我应打印本地pdf文件,如果定义了printer_name(普通字符串),程序应将文档发送到特定打印机,否则应使用默认打印机。

此代码有效,但无论如何都要使用默认打印机!

global printer_name
if printer_name:
    print "printer",nome_stampante
else:
    nome_stampante = win32print.GetDefaultPrinter()
    print "default printer"

out = '/d:"%s"' % nome_stampante

win32api.ShellExecute (
    0,
    "print",
    filename,
    out,
    ".",
    0
)

错误在哪里?

非常感谢

1 个答案:

答案 0 :(得分:2)

您的代码使用全局变量,没有人知道printer_name的设置位置以及nome_stampante中的内容。我建议你使用将使用所选打印机打印文件的功能。这看起来像:

import win32api
import win32print

def win_print(filename, printer_name = None):
    if not printer_name:
        printer_name = win32print.GetDefaultPrinter()
    out = '/d:"%s"' % (printer_name)
    win32api.ShellExecute(0, "print", filename, out, ".", 0)


def test_print():
    win_print('test.txt')
    win_print('test.txt', 'PDFCreator')


if __name__ == '__main__':
    test_print()