Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Retriving a table from excel sheet using python

$
0
0

Bills.xlsx

this is how the file bills.xlsx looks like,my goal is to create a method that takes a product name and then retrive all bills that contains that productHere is my code:

from openpyxl import Workbook, load_workbookfile_path = 'C:/Users/Mohamed Hamdi/Desktop/bills.xlsx'total_bills = []bills_workbook = load_workbook(file_path)bills_sheet = bills_workbook.activefor row in range(1, bills_sheet.max_row + 1):    bill_info = {'bill number': None, 'history': None, 'products': [], 'total price': None}    products = []    for col in range(1, 7):        cell_value = str(bills_sheet.cell(row=row, column=col).value)        if 'None' not in cell_value:  # Check if cell_value is not None and not empty            if "Bill Number" in cell_value:                products.append({'Bill Number': bills_sheet.cell(row=row, column=col + 1).value})            elif "History" in cell_value:                products.append({'History': bills_sheet.cell(row=row, column=col + 1).value})            elif "Total price" in cell_value:                products.append({'Total price': bills_sheet.cell(row=row, column=col + 1).value})            elif col==6:                products.append({'product name' : bills_sheet.cell(row=row, column=3).value})                 products.append({'count': bills_sheet.cell(row=row, column=4).value})                products.append({'price': bills_sheet.cell(row=row, column=5).value})                products.append({'total price': bills_sheet.cell(row=row, column=6).value})def find_rows_between_start_and_end(sheet_data, search_key, search_value, start_element, end_element):    # Initialize the list to store results    result_rows_list = []    # Loop through the sheet data to find all occurrences of the search value    for entries in sheet_data:        found_search_value = False        temp_result_rows = []        for idx, item in enumerate(entries):            if isinstance(item, dict) and search_key in item:                if item[search_key] == search_value:                    found_search_value = True                    search_index = idx                    start_index = None                    end_index = None                    # Find the first occurrence of the start element before the found element                    for j in range(search_index, -1, -1):                        if start_element in entries[j].values():                            start_index = j                            break                    # Find the first occurrence of the end element after the start element                    if start_index is not None:                        for k, sub_item in enumerate(entries[start_index:], start=start_index):                            if end_element in sub_item.values():                                end_index = k                                break                    # If both start and end elements are found, extract rows within the range                    if start_index is not None and end_index is not None:                        temp_result_rows.extend(entries[start_index:end_index + 1])        # If search value is found, add the collected rows to the result list        if found_search_value:            result_rows_list.append(temp_result_rows)    return result_rows_listsearch_key = "product name"search_value = "product"start_element = "Bill number"end_element = "Total price"  # Corrected end element nameresult_rows_list = find_rows_between_start_and_end(products, search_key, search_value, start_element, end_element)  # Pass 'data' instead of 'result'for result_rows in result_rows_list:    for row in result_rows:        print(row)

and this is what the array products looks like:

[{'Bill number': 31}, {'Date': '2024-04-16 04:39:44'}][{'product name': 'product1'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product1'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'Total price': 20}][][{'Bill number': 32}, {'Date': '2024-04-16 04:41:51'}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'Total price': 30}][][{'Bill number': 33}, {'Date': '2024-04-16 04:44:10'}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'Total price': 30}]

whwn I try to retrive the bills tabels it gives me nothing or incomplete data,If you have a direct way to retreive the data like the way I want please share it,the output I expect if I searched for product1 keyword:

[{'Bill number': 31}, {'Date': '2024-04-16 04:39:44'}][{'product name': 'product1'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'product name': 'product1'}, {'count': 1}, {'price': 10}, {'total price': 10}][{'Total price': 20}]

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>