Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,24 @@ import os
|
|
| 4 |
import ast
|
| 5 |
import streamlit as st
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def convert_pdf_to_excel(pdf_file):
|
| 9 |
inputpdf = PyPDF2.PdfReader(pdf_file)
|
|
@@ -21,6 +39,7 @@ def convert_pdf_to_excel(pdf_file):
|
|
| 21 |
date_qty = []
|
| 22 |
row_start_index = 0
|
| 23 |
row_stop_index = 0
|
|
|
|
| 24 |
for index in range(len(data)):
|
| 25 |
if data[index].strip() == 'Part No.':
|
| 26 |
each_table_data.append(data[index+1].replace('Part Color Code',""))
|
|
@@ -28,6 +47,13 @@ def convert_pdf_to_excel(pdf_file):
|
|
| 28 |
each_table_data.append(data[index+2].replace('Part Color Code',""))
|
| 29 |
else:
|
| 30 |
each_table_data.append("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if 'Part Name' in data[index].strip():
|
| 33 |
each_table_data.append(data[index+1])
|
|
@@ -41,7 +67,7 @@ def convert_pdf_to_excel(pdf_file):
|
|
| 41 |
if row_start_index>0 and row_stop_index>0:
|
| 42 |
for index in range(row_start_index,row_stop_index):
|
| 43 |
if '/' in data[index].strip():
|
| 44 |
-
date_qty.append([data[index].strip()[-5:].strip(),data[index+1].strip()])
|
| 45 |
if not date_qty:
|
| 46 |
date_qty = [["",""]]
|
| 47 |
each_table_data.append(date_qty)
|
|
@@ -61,6 +87,8 @@ def map_data_to_template(excel_file, mapping_file):
|
|
| 61 |
# Load Excel file and mapping file
|
| 62 |
extracted_data = pd.read_excel(excel_file)
|
| 63 |
mapping_data = pd.read_excel(mapping_file)
|
|
|
|
|
|
|
| 64 |
mapping_data = mapping_data.rename(columns = {'Customer Part no as per pdf':'Part No.'})
|
| 65 |
|
| 66 |
# Perform mapping
|
|
@@ -68,10 +96,68 @@ def map_data_to_template(excel_file, mapping_file):
|
|
| 68 |
extracted_data = extracted_data.explode('Date Qty')
|
| 69 |
extracted_data[['SchDate','Qty']]= pd.DataFrame(extracted_data['Date Qty'].to_list(), index= extracted_data.index)
|
| 70 |
extracted_data = extracted_data.drop('Date Qty',axis=1)
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return mapped_data
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def main():
|
| 76 |
st.title("PDF to Excel Converter")
|
| 77 |
|
|
@@ -99,12 +185,26 @@ def main():
|
|
| 99 |
else:
|
| 100 |
st.error("Error: Converted Excel file not found")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
if mapping_uploaded_file is not None:
|
| 107 |
-
st.write("Uploaded Mapping Excel file:", mapping_uploaded_file.name)
|
| 108 |
|
| 109 |
# Perform data mapping
|
| 110 |
mapped_data = map_data_to_template(extracted_file, mapping_uploaded_file)
|
|
@@ -126,10 +226,6 @@ def main():
|
|
| 126 |
)
|
| 127 |
else:
|
| 128 |
st.error("Error: Converted Excel file not found")
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
if __name__ == "__main__":
|
| 135 |
main()
|
|
|
|
| 4 |
import ast
|
| 5 |
import streamlit as st
|
| 6 |
import pandas as pd
|
| 7 |
+
import os
|
| 8 |
+
from google.oauth2.credentials import Credentials
|
| 9 |
+
from google.auth.transport.requests import Request
|
| 10 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 11 |
+
from googleapiclient.discovery import build
|
| 12 |
+
from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
|
| 13 |
+
|
| 14 |
+
# Load credentials from environment variables
|
| 15 |
+
credentials_dict = {
|
| 16 |
+
"token": os.environ.get("token"),
|
| 17 |
+
"refresh_token": os.environ.get("refresh_token"),
|
| 18 |
+
"token_uri": os.environ.get("token_uri"),
|
| 19 |
+
"client_id": os.environ.get("client_id"),
|
| 20 |
+
"client_secret": os.environ.get("client_secret"),
|
| 21 |
+
"scopes": os.environ.get("scopes")
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
MAPPING_FILENAME = "Data Mapping with ItemCode.xlsx"
|
| 25 |
|
| 26 |
def convert_pdf_to_excel(pdf_file):
|
| 27 |
inputpdf = PyPDF2.PdfReader(pdf_file)
|
|
|
|
| 39 |
date_qty = []
|
| 40 |
row_start_index = 0
|
| 41 |
row_stop_index = 0
|
| 42 |
+
year = ""
|
| 43 |
for index in range(len(data)):
|
| 44 |
if data[index].strip() == 'Part No.':
|
| 45 |
each_table_data.append(data[index+1].replace('Part Color Code',""))
|
|
|
|
| 47 |
each_table_data.append(data[index+2].replace('Part Color Code',""))
|
| 48 |
else:
|
| 49 |
each_table_data.append("")
|
| 50 |
+
|
| 51 |
+
if data[index].strip()=='MORIROKU TECHNOLOGY':
|
| 52 |
+
try:
|
| 53 |
+
year = data[index+1].split(' ')[0].split('/')[1]
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(e)
|
| 56 |
+
year = ""
|
| 57 |
|
| 58 |
if 'Part Name' in data[index].strip():
|
| 59 |
each_table_data.append(data[index+1])
|
|
|
|
| 67 |
if row_start_index>0 and row_stop_index>0:
|
| 68 |
for index in range(row_start_index,row_stop_index):
|
| 69 |
if '/' in data[index].strip():
|
| 70 |
+
date_qty.append([data[index].strip()[-5:].strip() + "/"+year,data[index+1].strip()])
|
| 71 |
if not date_qty:
|
| 72 |
date_qty = [["",""]]
|
| 73 |
each_table_data.append(date_qty)
|
|
|
|
| 87 |
# Load Excel file and mapping file
|
| 88 |
extracted_data = pd.read_excel(excel_file)
|
| 89 |
mapping_data = pd.read_excel(mapping_file)
|
| 90 |
+
mapping_data.to_excel(MAPPING_FILENAME)
|
| 91 |
+
save_mapping_file_to_drive()
|
| 92 |
mapping_data = mapping_data.rename(columns = {'Customer Part no as per pdf':'Part No.'})
|
| 93 |
|
| 94 |
# Perform mapping
|
|
|
|
| 96 |
extracted_data = extracted_data.explode('Date Qty')
|
| 97 |
extracted_data[['SchDate','Qty']]= pd.DataFrame(extracted_data['Date Qty'].to_list(), index= extracted_data.index)
|
| 98 |
extracted_data = extracted_data.drop('Date Qty',axis=1)
|
| 99 |
+
extracted_data = extracted_data[~extracted_data['SchDate'].isna()]
|
| 100 |
+
mapped_data = extracted_data.merge(mapping_data, on =['Part No.'],how='outer')#[['Item Code','SchDate','Qty']]
|
| 101 |
+
mapped_data['SOType'] = "R"
|
| 102 |
+
mapped_data = mapped_data[~mapped_data["SchDate"].isna()]
|
| 103 |
|
| 104 |
return mapped_data
|
| 105 |
|
| 106 |
+
def save_mapping_file_to_drive():
|
| 107 |
+
creds = Credentials.from_authorized_user_info(credentials_dict)
|
| 108 |
+
# Authenticate with Google Drive API
|
| 109 |
+
service = build('drive', 'v3', credentials=creds)
|
| 110 |
+
folder_id = "1HBRUZePST0D0buyU9MxeYg2vQyEL4wLF"
|
| 111 |
+
|
| 112 |
+
# List all files in the folder
|
| 113 |
+
results = service.files().list(
|
| 114 |
+
q=f"'{folder_id}' in parents and mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
|
| 115 |
+
fields="files(id, name)").execute()
|
| 116 |
+
files = results.get('files', [])
|
| 117 |
+
files = [i for i in files if i.get('name')=='Data Mapping with ItemCode.xlsx']
|
| 118 |
+
|
| 119 |
+
if not files:
|
| 120 |
+
print('No Excel Mapping files found in the folder.')
|
| 121 |
+
else:
|
| 122 |
+
for file in files:
|
| 123 |
+
# Get the ID and name of the first Excel file found in the folder
|
| 124 |
+
existing_file_id = file['id']
|
| 125 |
+
existing_file_name = file['name']
|
| 126 |
+
|
| 127 |
+
# Delete the existing file
|
| 128 |
+
service.files().delete(fileId=existing_file_id).execute()
|
| 129 |
+
|
| 130 |
+
file_metadata = {'name': MAPPING_FILENAME, 'parents': [folder_id]}
|
| 131 |
+
media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
| 132 |
+
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 133 |
+
|
| 134 |
+
def pull_mapping_file_from_drive():
|
| 135 |
+
creds = Credentials.from_authorized_user_info(credentials_dict)
|
| 136 |
+
# Authenticate with Google Drive API
|
| 137 |
+
service = build('drive', 'v3', credentials=creds)
|
| 138 |
+
|
| 139 |
+
results = service.files().list(
|
| 140 |
+
q="mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
|
| 141 |
+
fields="files(id, name)").execute()
|
| 142 |
+
files = results.get('files', [])
|
| 143 |
+
files = [i for i in files if i.get('name')=='Data Mapping with ItemCode.xlsx']
|
| 144 |
+
if files:
|
| 145 |
+
file_id = files[0]['id']
|
| 146 |
+
file_name = files[0]['name']
|
| 147 |
+
request = service.files().get_media(fileId=file_id)
|
| 148 |
+
fh = open(file_name, 'wb')
|
| 149 |
+
downloader = MediaIoBaseDownload(fh, request)
|
| 150 |
+
|
| 151 |
+
# Execute the download
|
| 152 |
+
done = False
|
| 153 |
+
while not done:
|
| 154 |
+
status, done = downloader.next_chunk()
|
| 155 |
+
|
| 156 |
+
fh.close()
|
| 157 |
+
return 1
|
| 158 |
+
return 0
|
| 159 |
+
|
| 160 |
+
|
| 161 |
def main():
|
| 162 |
st.title("PDF to Excel Converter")
|
| 163 |
|
|
|
|
| 185 |
else:
|
| 186 |
st.error("Error: Converted Excel file not found")
|
| 187 |
|
| 188 |
+
file_present = pull_mapping_file_from_drive()
|
| 189 |
+
if not os.path.exists("Data Mapping with ItemCode.xlsx"):
|
| 190 |
+
st.markdown("## Upload the Data Master file with Item Code mapping")
|
| 191 |
+
mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
|
| 192 |
+
else:
|
| 193 |
+
mapping_data = pd.read_excel("Data Mapping with ItemCode.xlsx")
|
| 194 |
+
mapping_data = mapping_data.rename(columns = {'Customer Part no as per pdf':'Part No.'})
|
| 195 |
+
data_for_mapping = "Data Mapping.xlsx"
|
| 196 |
+
extracted_data_for_mapping = pd.read_excel(data_for_mapping)
|
| 197 |
+
extracted_data_for_mapping = extracted_data_for_mapping[~extracted_data_for_mapping['Part No.'].isin(mapping_data['Part No.'])]
|
| 198 |
+
unmapped_part_no = extracted_data_for_mapping['Part No.'].nunique()
|
| 199 |
+
if unmapped_part_no>0:
|
| 200 |
+
st.markdown("#### There are {} Part No. with No ItemCode present. Upload a new file after mapping them".format(unmapped_part_no))
|
| 201 |
+
mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
|
| 202 |
+
else:
|
| 203 |
+
st.markdown("#### Using the Mapping file available in Google Drive")
|
| 204 |
+
mapping_uploaded_file = "Data Mapping with ItemCode.xlsx"
|
| 205 |
|
| 206 |
if mapping_uploaded_file is not None:
|
| 207 |
+
# st.write("Uploaded Mapping Excel file:", mapping_uploaded_file.name)
|
| 208 |
|
| 209 |
# Perform data mapping
|
| 210 |
mapped_data = map_data_to_template(extracted_file, mapping_uploaded_file)
|
|
|
|
| 226 |
)
|
| 227 |
else:
|
| 228 |
st.error("Error: Converted Excel file not found")
|
| 229 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
if __name__ == "__main__":
|
| 231 |
main()
|