Python Forum
beginner level Python:- output XLS spreadsheet instead of using tabulate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner level Python:- output XLS spreadsheet instead of using tabulate
#6
Took me all day but got there in the end..
If there is a better way please comment (totally new to this)

"""
Simple application that logs on to the APIC and displays all
of the Endpoints.
"""
import acitoolkit.acitoolkit as aci
import xlwt
from datetime import datetime
import time


def main():
    """
    Main Show Endpoints Routine
    :return: None
    """
    # Get timestamp for filename output
    now = time.time()
    local_time = time.localtime(now)
    timestamp = time.strftime("%Y-%m-%d-%H%M", local_time)
    
    # Take login credentials from the command line if provided
    # Otherwise, take them from your environment variables file ~/.profile
    description = ('Simple application that logs on to the APIC'
                   ' and displays all of the Endpoints.')
    creds = aci.Credentials('apic', description)
    args = creds.get()

    # Login to APIC
    session = aci.Session(args.url, args.login, args.password)
    resp = session.login()
    if not resp.ok:
        print('%% Could not login to APIC')
        return

    # Download all of the interfaces
    # and store the data as tuples in a list
    data = []
    endpoints = aci.Endpoint.get(session)
    for ep in endpoints:
        epg = ep.get_parent()
        app_profile = epg.get_parent()
        tenant = app_profile.get_parent()
        data.append((ep.mac, ep.ip, ep.if_name, ep.encap,
                     tenant.name, app_profile.name, epg.name))

    # Write downloaded data to ACI-Endpoints.xls
	result_excel_file_name = "ACI-Endpoints-" + timestamp + ".xls"
	headers=["MACADDRESS", "IPADDRESS", "INTERFACE",  "ENCAP", "TENANT", "APP PROFILE", "EPG"]

	wb = xlwt.Workbook(encoding='utf-8')
	ws = wb.add_sheet('EndPoints')
	
    rowx = 0
    for colx, value in enumerate(headers):
        ws.write(rowx, colx, value)
    ws.set_panes_frozen(True)  # frozen headings instead of split panes
    ws.set_horz_split_pos(rowx + 1)  # in general, freeze after last heading row
    ws.set_remove_splits(True)  # if user does unfreeze, don't leave a split there
    for row in data:
        rowx += 1
        for colx, value in enumerate(row):
        	ws.write(rowx, colx, value.encode('utf-8').decode('utf-8'))
    wb.save(result_excel_file_name)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        pass
Reply


Messages In This Thread
RE: beginner level Python:- output XLS spreadsheet instead of using tabulate - by haziebaby - Jan-27-2018, 07:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting of rows in Excel Spreadsheet dakrantau 2 1,945 Sep-30-2020, 02:27 AM
Last Post: dakrantau
  How do I write a line of code into an excel spreadsheet using Python code? Emerogork 2 3,156 Feb-07-2018, 06:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020