Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import string
  2. import time
  3.  
  4. from openpyxl import Workbook
  5. from openpyxl.styles import Alignment, Font, NamedStyle, PatternFill
  6. wb = Workbook()
  7. ws = wb.active
  8. ws.title = 'Access Points'
  9.  
  10. header = {'Network': 16, 'Address': 20.43, 'AP-Name': 29, 'Tags': 29,
  11. 'IP Address': 19, 'Gateway': 19, 'DNS': 20.57, 'AP Model': 18.29,
  12. 'Status': 18.29, 'Serial': 21.29, 'MAC': 18.57}
  13.  
  14.  
  15. def stamp():
  16. now = time.localtime()
  17. now_val = ('%s%s%s%s%s%s') % (now[1], now[2], now[0], now[3], now[4],
  18. now[5])
  19. return now_val
  20.  
  21.  
  22. def create_header(ws):
  23. ws.append(list(header.copy().keys()))
  24.  
  25.  
  26. def cell_format(ws):
  27. headstyle = NamedStyle(name='headstyle')
  28. headstyle.font = Font(bold=True, name='Times New Roman')
  29. headstyle.alignment = Alignment(horizontal='center')
  30. headfill = PatternFill(start_color='ddd9c4', end_color='ddd9c4',
  31. fill_type='solid')
  32. # blah = [cell.value for cell in ws[1]]
  33. for cell in ws[1]:
  34. cell.style = headstyle
  35. cell.fill = headfill
  36.  
  37.  
  38. def set_widths(ws, header):
  39. for col in string.ascii_uppercase[:len(header.keys())]:
  40. # ws.column_dimensions['A'].width = 200
  41. # print(col)
  42. headval = ws['{}{}'.format(col, '1')].value
  43. widthval = (header[headval])
  44. ws.column_dimensions[col].width = widthval
  45.  
  46.  
  47. def find_header(search_string):
  48. """Returns col number for searched value in first row."""
  49. from openpyxl.utils import column_index_from_string
  50. myheader = ws[1]
  51. for item in myheader:
  52. if search_string in item.value:
  53. print(item.value)
  54. alpha_col = item.column
  55. num_col = column_index_from_string(item.column)
  56. return(num_col, alpha_col)
  57.  
  58.  
  59. def write_serials(serials, num_col):
  60. # set our first row to be after the header
  61. rrow = 2
  62. for item in serials:
  63. ws.cell(column=num_col, row=rrow, value=item)
  64. rrow = rrow + 1
  65. print(rrow)
  66.  
  67.  
  68. myserials = ['Q2QW-5Y9P-3C3Y', 'Q2QW-5VEB-MXQW']
  69. create_header(ws)
  70. cell_format(ws)
  71. set_widths(ws, header)
  72. num_col, alpha_col = find_header('Serial')
  73. write_serials(myserials, num_col)
  74.  
  75. # sys.exit()
  76. fileBase = 'Meraki-API'
  77. fileName = fileBase + '_' + stamp() + '.xlsx'
  78. # print(fileName)
  79. wb.save('C:\\temp\\clean-daily\\{}'.format(fileName))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement