From 684cf74cc364dd3c94e0822d634b365c6eb57dc0 Mon Sep 17 00:00:00 2001 From: Sugiarto Date: Thu, 7 Dec 2023 10:28:55 +0700 Subject: [PATCH] update pdfexcel with reading column anomaly --- main.py | 3 ++- pdfexcel4DNwithxlrd.py | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index a2976d6..8c4e34c 100644 --- a/main.py +++ b/main.py @@ -60,6 +60,7 @@ async def renew_iteminventory(): @app.post("/upload-file/") async def create_upload_file(uploaded_file: UploadFile): base_file_location = f"DN_Excel_files" + status = "ERROR" if uploaded_file.filename.endswith(".xls") and uploaded_file.filename.startswith("TCO-DN"): folder_yearmonth = uploaded_file.filename.split('-') if len(folder_yearmonth)>3: @@ -127,7 +128,7 @@ async def create_upload_file(uploaded_file: UploadFile): return [{"info": f"file '{uploaded_file.filename}' saved at '{file_location}'", "status": status, "msg": msg}] - return {"info": f"file '{uploaded_file.filename}' is not Excel(.xls) file or it is not Delivery Note(TCO-DN)file. please upload the correct file", "status": status} + return {"info": f"file '{uploaded_file.filename}' is not Excel(.xls) file or Filename is not start with 'TCO-DN'. Please upload the correct file", "status": status} @app.post('/getopenso') async def getopenso(request: Request, CustomerName: str = Body(...)): diff --git a/pdfexcel4DNwithxlrd.py b/pdfexcel4DNwithxlrd.py index dd44422..f86c46e 100644 --- a/pdfexcel4DNwithxlrd.py +++ b/pdfexcel4DNwithxlrd.py @@ -84,9 +84,25 @@ def read_DN_excel(filename): # DNRefNum = rawdata[1][3].strip().split("-")[1][-1] + "".join(rawdata[1][3].strip().split("-")[-2:]) # TxnDate = rawdata[2][3].strip().split(" ")[-1] - DNRefNum = wb.sheets()[0].cell(1, 3).value.strip().split("-")[1][-1] + "".join(wb.sheets()[0].cell(1, 3).value.strip().split("-")[-2:]) + _y=1 + _x=0 + for col in range(1,wb.sheets()[0].ncols): + + col_value = wb.sheets()[0].col_values(col) + print("col=",col, col_value[1]) + if "TCO-DN" in col_value[1]: + _x=col + if _x == 0 : + return False, "Cannot find TCO-DN cell" + + + # if "TCO-DN" in wb.sheets()[0].cell(_y, _x).value.strip(): + + + print(wb.sheets()[0].cell(_y, _x).value.strip()) + DNRefNum = wb.sheets()[0].cell(_y, _x).value.strip().split("-")[1][-1] + "".join(wb.sheets()[0].cell(_y, _x).value.strip().split("-")[-2:]) # print(DNRefNum) - TxnDate = wb.sheets()[0].cell(2, 3).value.strip().split(" ")[-1] + TxnDate = wb.sheets()[0].cell(_y+1, _x).value.strip().split(" ")[-1] Memo = DNRefNum print(f'pdfexcel4DNwithxlrd.py->DNRefNum:{DNRefNum}, TxnDate:{TxnDate}') DeliveryNotedict={'DNRefNum':DNRefNum, 'TxnDate':TxnDate, 'Memo': Memo}