Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # Đường dẫn đến file Excel | |
| excel_file = 'product.xlsx' | |
| # Tên của sheet bạn muốn đọc | |
| def check_product(product_id): | |
| sheet_name_used = 'Cấm NK cũ' | |
| sheet_name_new = 'Cấm NK mới' | |
| sheet_name_null = 'Cấm NK tất cả' | |
| # Đọc sheet từ file Excel | |
| df_used = pd.read_excel( | |
| excel_file, sheet_name=sheet_name_used, header=None, dtype=str) | |
| df_new = pd.read_excel( | |
| excel_file, sheet_name=sheet_name_new, header=None, dtype=str) | |
| df_null = pd.read_excel( | |
| excel_file, sheet_name=sheet_name_null, header=None, dtype=str) | |
| # Tìm sản phẩm trong DataFrame | |
| product_row_used = df_used[df_used[0] == product_id] | |
| product_row_new = df_new[df_new[0] == product_id] | |
| product_row_null = df_null[df_null[0] == product_id] | |
| if not product_row_used.empty: | |
| return "Cấm nhập khẩu hàng cũ", product_row_used | |
| elif not product_row_new.empty: | |
| return "Cấm nhập khẩu hàng mới", product_row_new | |
| elif not product_row_null.empty: | |
| return "Cấm nhập khẩu cả cũ và mới", product_row_null | |
| else: | |
| return "Xuất nhập khẩu bình thường", None | |
| def check_product_all_sheet(product_id): | |
| # Read all sheets in the Excel file | |
| xls = pd.ExcelFile(excel_file) | |
| # List to store the names of the sheets where the product is found | |
| found_sheets = [] | |
| product_rows = [] | |
| # Iterate over each sheet | |
| for sheet_name in xls.sheet_names: | |
| df = pd.read_excel(xls, sheet_name=sheet_name, header=None, dtype=str) | |
| df = df.fillna(method='ffill', axis=0) | |
| # Find the product in the DataFrame | |
| product_row = df[df[0] == product_id] | |
| # If the product is found, add the sheet name to the list | |
| if not product_row.empty: | |
| found_sheets.append(sheet_name) | |
| # product_row['Status'] = sheet_name | |
| product_rows.append(product_row) | |
| # If the product is found in at least one sheet, return the list of sheet names | |
| if found_sheets: | |
| return "<br/>".join(found_sheets), product_rows | |
| # If the product is not found in any sheet, return a message | |
| return "Xuất nhập khẩu bình thường", None | |
| def check_products(txt: str): | |
| product_ids = txt.split('\n') | |
| # Tạo list để lưu kết quả | |
| result_list = [] | |
| # Kiểm tra từng sản phẩm | |
| for product_id in product_ids: | |
| status, product_info = check_product_all_sheet(product_id) | |
| print(f"Sản phẩm {product_id}: {status} : {product_info}") | |
| # Thêm trạng thái vào thông tin sản phẩm | |
| if product_info is not None: | |
| # product_info['Status'] = status | |
| # result_list.append(product_info) | |
| result_list.extend(product_info) | |
| else: | |
| result_list.append(pd.DataFrame( | |
| {0: [product_id], 3: [status]})) | |
| # Ghép tất cả các hàng của sản phẩm | |
| result_df = pd.concat(result_list) | |
| # Ghi DataFrame vào file Excel | |
| # result_df.to_excel('result.xlsx', index=False) | |
| return result_df | |
| def main(hs_ids: str): | |
| print(hs_ids) | |
| # check products form hs_ids return to gradio | |
| res = check_products(hs_ids) | |
| file_path = 'result.xlsx' | |
| res.to_excel(file_path, index=False) | |
| return res.to_markdown(index=False), file_path | |