Skip to content

Instantly share code, notes, and snippets.

@Ch3mjor
Created June 10, 2019 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ch3mjor/5ffba48c0658e2b98215c975a0d1a1d2 to your computer and use it in GitHub Desktop.
Save Ch3mjor/5ffba48c0658e2b98215c975a0d1a1d2 to your computer and use it in GitHub Desktop.
Get information from one column in one sheet and match it to another column in another sheet, and extract it as a csv file.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment