Created
May 22, 2020 07:00
-
-
Save rdapaz/1475f99a61ff59e1b70b632ca953654a to your computer and use it in GitHub Desktop.
Using Jinja2 inline template to generate cisco switch changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import win32com.client as c | |
from jinja2 import FileSystemLoader, Environment, Template | |
xlApp = c.gencache.EnsureDispatch('Excel.Application') | |
xlApp.Visible = True | |
path = r'path.xlsx' | |
wk = xlApp.Workbooks.Open(path) | |
sh = wk.Worksheets('Sheet1 (2)') | |
subs = """ | |
Switch1|Engineering-switch | |
Switch2|Admin-switch | |
""".splitlines() | |
subs = {k: v for k, v in [y.split('|') for y in subs if len(y) > 0]} | |
tt = """ | |
{{new_switch}} | |
{%- for p in interface_data %} | |
interface {{p['ifce']}} | |
desc {{p['new_desc']}} | |
! | |
{%- endfor %} | |
""" | |
eof = sh.Range('A65535').End(-4162).Row | |
ifce_data = [] | |
switches = set() | |
for row in range(2,eof+1): | |
switch = sh.Range(f'A{row}').Value | |
ifce = sh.Range(f'B{row}').Value | |
old_desc = sh.Range(f'C{row}').Value | |
new_desc = sh.Range(f'I{row}').Value | |
if sh.Range(f'A{row}').Interior.ColorIndex == 6: | |
ifce_data.append(dict(switch=switch, ifce=ifce, old_desc=old_desc, new_desc=new_desc)) | |
switches.add(switch) | |
env = Environment() | |
template = env.from_string(tt) | |
switches = sorted(list(switches)) | |
for switch in switches: | |
new_switch = switch | |
ifce_byswitch = [plist for plist in ifce_data if plist['switch'] == switch ] | |
print(template.render(switch=switch, new_switch=new_switch, interface_data=ifce_byswitch)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment