I need to grab the printer IP address and printer name from a spreadsheet. 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? I've been able to open and read the file using xlrd
with the following code:
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?
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.
Question : What I need to do is grab the printer name, and IP address, insert them into a 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.