使用 Python 在 OS X 中获取所有可用的打印机
Get all available printers in OS X using Python
目前我正在对 Python 中的打印机进行一些测试,我想做的是列出所有可用的打印机。
现在我正在使用 PyCups 库,它在 Connection
class 中公开了几个有用的 API。其中还有 getPrinters()
:
这是我使用并有效的片段:
>>> import cups
>>> conn = cups.Connection ()
>>> printers = conn.getPrinters ()
>>> for printer in printers:
... print printer, printers[printer]["device-uri"]
Brother_MFC_1910W_series
Photosmart_6520_series
我想知道是否有什么方法可以在不使用任何外部库的情况下编写上述代码。我很确定如果不使用 C 就无法做到这一点。
任何对文档的建议或参考将不胜感激。谢谢
我正在使用Python3
您可以使用终端命令 lpstat
(man for OSX) 执行类似的查询。 Python 的内置 subprocess
模块将允许您 运行 该命令并存储输出。某些文本解析应提供打印机名称。
from subprocess import Popen, PIPE
# "lpstat -a" prints info on printers that can accept print requests
p = Popen(['lpstat', '-a'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
lines = output.split('\n')
# check before you implement this parsing
printers = map(lambda x: x.split(' ')[0], lines)
可以仅将 Python 中的 C 库与标准模块一起使用。参考文献:CUPS API, ctypes。将 CUPS 结构和调用转换为 ctypes
语法,我们得到一个在标准 OS X Python 和 Python 3:
下工作的代码
from __future__ import print_function
from ctypes import *
class cups_option_t(Structure):
_fields_ = [
('name', c_char_p),
('value', c_char_p)
]
class cups_dest_t(Structure):
_fields_ = [
('name', c_char_p),
('instance', c_char_p),
('is_default', c_int),
('num_options', c_int),
('cups_option_t', POINTER(cups_option_t))
]
cups_lib = cdll.LoadLibrary('/usr/lib/libcups.dylib')
if __name__ == '__main__':
dests = cups_dest_t()
dests_p = pointer(dests)
num_dests = cups_lib.cupsGetDests(byref(dests_p))
for i in range(num_dests):
dest = dests_p[i]
print(dest.is_default, dest.name)
for j in range(dest.num_options):
option = dest.cups_option_t[j]
print('', option.name, option.value, sep='\t')
cups_lib.cupsFreeDests(num_dests, dests_p)
使用ctypes
时要特别小心,大多数错误都会产生分段错误。
目前我正在对 Python 中的打印机进行一些测试,我想做的是列出所有可用的打印机。
现在我正在使用 PyCups 库,它在 Connection
class 中公开了几个有用的 API。其中还有 getPrinters()
:
这是我使用并有效的片段:
>>> import cups
>>> conn = cups.Connection ()
>>> printers = conn.getPrinters ()
>>> for printer in printers:
... print printer, printers[printer]["device-uri"]
Brother_MFC_1910W_series
Photosmart_6520_series
我想知道是否有什么方法可以在不使用任何外部库的情况下编写上述代码。我很确定如果不使用 C 就无法做到这一点。
任何对文档的建议或参考将不胜感激。谢谢
我正在使用Python3
您可以使用终端命令 lpstat
(man for OSX) 执行类似的查询。 Python 的内置 subprocess
模块将允许您 运行 该命令并存储输出。某些文本解析应提供打印机名称。
from subprocess import Popen, PIPE
# "lpstat -a" prints info on printers that can accept print requests
p = Popen(['lpstat', '-a'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
lines = output.split('\n')
# check before you implement this parsing
printers = map(lambda x: x.split(' ')[0], lines)
可以仅将 Python 中的 C 库与标准模块一起使用。参考文献:CUPS API, ctypes。将 CUPS 结构和调用转换为 ctypes
语法,我们得到一个在标准 OS X Python 和 Python 3:
from __future__ import print_function
from ctypes import *
class cups_option_t(Structure):
_fields_ = [
('name', c_char_p),
('value', c_char_p)
]
class cups_dest_t(Structure):
_fields_ = [
('name', c_char_p),
('instance', c_char_p),
('is_default', c_int),
('num_options', c_int),
('cups_option_t', POINTER(cups_option_t))
]
cups_lib = cdll.LoadLibrary('/usr/lib/libcups.dylib')
if __name__ == '__main__':
dests = cups_dest_t()
dests_p = pointer(dests)
num_dests = cups_lib.cupsGetDests(byref(dests_p))
for i in range(num_dests):
dest = dests_p[i]
print(dest.is_default, dest.name)
for j in range(dest.num_options):
option = dest.cups_option_t[j]
print('', option.name, option.value, sep='\t')
cups_lib.cupsFreeDests(num_dests, dests_p)
使用ctypes
时要特别小心,大多数错误都会产生分段错误。