简体   繁体   English

从电子表格中获取打印机IP和打印机名称

[英]Grabbing the printer IP and printer name from a spreadsheet

I need to grab the printer IP address and printer name from a spreadsheet. 我需要从电子表格中获取打印机IP地址和打印机名称。 How this spreadsheet looks: 该电子表格的外观:

Printer Model    Printer Name          Current IP       MAC     New IP
some printer     XRX0000AAF3230C       166.96.64.51     sad     127.0.1.1
example          NPIB36BA6             172.18.25.126    sad     255.255.255.0

How can I grab the IP address and the printer name, and insert them into a dict? 如何获取IP地址和打印机名称,并将其插入字典中? I've been able to open and read the file using xlrd with the following code: 我已经可以使用xlrd通过以下代码打开和读取文件:

book = xlrd.open_workbook(r"C:\Users\Documents\My Forms\Printers 3-14-2017.xlsx", encoding_override="cp1252")
sheet = book.sheet_by_name("3rd Floor")
cols = sheet.ncols - 1
rows = sheet.nrows - 1
curr_row = -1
while curr_row < rows:
    curr_row += 1
    row = sheet.row(curr_row)
    print("Row: {}".format(curr_row))
    curr_cell = -1
    while curr_cell < cols:
        curr_cell += 1
        cell_type = sheet.cell_type(curr_row, curr_cell)
        cell_val = sheet.cell_value(curr_row, curr_cell)
        try:
            print("Type: {}\nValue: {}\n".format(cell_type, cell_val))
        except UnicodeEncodeError:
            print("Type: {}\nValue: {}\n".format(smart_str(cell_type), smart_str(cell_val)))

What I need to do is grab the printer name, and IP address, insert them into a dict and output that dict, how can I do this successfully and skip the other information, and the "new IP" section? 我需要做的是获取打印机名称和IP地址,将它们插入字典并输出该字典,如何才能成功完成并跳过其他信息,以及“新IP”部分?

I thought about using regular expressions, this could work with the IP address, but there is not pattern in the printer name apart from printer types, such as Xerox printers start with XRX , HP printers start with NP or HP , etc.. Any help with this would be extremely appreciated, thank you. 我考虑过使用正则表达式,它可以使用IP地址,但是除了打印机类型外,打印机名称中没有模式,例如Xerox打印机以XRX ,HP打印机以NPHP开头等。任何帮助非常感谢,谢谢。

Question : What I need to do is grab the printer name, and IP address, insert them into a dic ... :我需要做的是获取打印机名称和IP地址,将它们插入dic ...

my_dict['printer'] = sheet.cell_value(curr_row, 2)
my_dict['IP address'] = sheet.cell_value(curr_row, 3)

Columns are counted 1 based from left to right 列从左到右计为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM