Amr Sohil
Amr Sohil

Reputation: 105

Building a simple Information Database using python & Excel

I'm working on a simple information DB using python and excel, currently i have excel workbook (db.xlsx) containing 1 sheet with 3 columns and x rows. columns: (A:employee, B:password, C:email)

the python app should get the user input ("Employee Name") and search for the entered string in column A if found then display the ("password & email") from column B and C

ex:

excel file data:

A, B, C

employee, password, email

john doe, 12345, [email protected]

Johanna doe, 978765, [email protected]

User input: john doe

employee name: john doe - password : 12345, email: [email protected]

also i would like the app to ask me to add new record if employee name is not in the excel file.

this is the code i wrote so far, i cannot continue to write the rest of the code as i'm stuck in the search & display data part.

from openpyxl import *
wb = load_workbook('db.xlsx')
sheet = wb.get_sheet_by_name('QA')

q1 = raw_input('Enter Employee Name: ')
for i in range(1, sheet.max_row):
    if sheet.cell(row=row, column=0).value == q1:
        for j in range(i, sheet.max_column):
            print (sheet.cell(row=i, column=j).value)

your help is kindly appreciated.

Update: This is the working code after changing #if sheet.cell(row=row, column=0).value == q1: to if sheet.cell(row=row, column=1).value == q1: as Gordon13 suggested, Thanks

from openpyxl import *
wb = load_workbook('db.xlsx')
sheet = wb.get_sheet_by_name('QA')
while True:
    q1 = raw_input('Input: ')
    for row in range(1, sheet.max_row):
        if sheet.cell(row=row , column=1).value == str(q1):
            print sheet.cell(row=row, column=2).value
            print sheet.cell(row=row, column=3).value

Upvotes: 1

Views: 364

Answers (1)

Gordon13
Gordon13

Reputation: 487

Can you explain exactly what you're seeing when you run your code?

I think I see a small issue with your loop indices, but I can't be sure unless you describe what's happening

This is untested, but I think it should be closer to what you want:

from openpyxl import *
wb = load_workbook('db.xlsx')
sheet = wb.get_sheet_by_name('QA')

q1 = raw_input('Enter Employee Name: ')
for i in range(1, sheet.max_row):
    # changed 'row' to i
    if sheet.cell(row=i, column=0).value == q1:
        # changed i to 1
        for j in range(1, sheet.max_column):
            print (sheet.cell(row=i, column=j).value)

Upvotes: 0

Related Questions