Skip to content

Instantly share code, notes, and snippets.

@Ch3mjor
Created June 10, 2019 14:49
Show Gist options
  • Select an option

  • Save Ch3mjor/5ffba48c0658e2b98215c975a0d1a1d2 to your computer and use it in GitHub Desktop.

Select an option

Save Ch3mjor/5ffba48c0658e2b98215c975a0d1a1d2 to your computer and use it in GitHub Desktop.

Revisions

  1. Ch3mjor created this gist Jun 10, 2019.
    42 changes: 42 additions & 0 deletions Excel_Python.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import xlrd
    from csv import writer

    def file_writing(client):
    #Function to write client details to a csv file (later to import into excel)
    with open("clients.csv", mode = "a") as file:
    csv_writer = writer(file)
    csv_writer.writerow(client)


    def client_id_name():
    #replace this location with the path to your Excel document
    location = ("Path")
    work_book = xlrd.open_workbook(location)
    sheet1 = work_book.sheet_by_index(4)
    #print(sheet1.nrows)
    #printing all columns
    for i in range(sheet1.nrows):
    #obtaining client id and saving it in a variable called client_id
    client_id = sheet1.cell_value(i,0)
    sheet2 = work_book.sheet_by_index(9)
    #obtaining client name from SMSC and comparing client id for similarity
    #Count Variable to keep count of details found or not
    count = 0
    for j in range(sheet2.nrows):
    table2_id = sheet2.cell_value(j,1)
    #Obtaining client Name
    if client_id == table2_id:
    client_name = sheet2.cell_value(j,2)
    print(f"{client_id} {client_name}")
    count = 1
    #passing client name to csv
    file_writing(client_name)
    #break
    elif (sheet2.nrows - j) == 1 and client_id != table2_id and count == 0:
    #Catching Cells with no equivalent in the other Sheet
    print(f"Error!!!{client_id}")
    #passing client name to csv
    client_name = "NONE FOUND"
    file_writing(client_name)

    client_id_name()