Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,12 @@ from google_auth_oauthlib.flow import InstalledAppFlow
|
|
11 |
from googleapiclient.discovery import build
|
12 |
from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
|
13 |
from google.oauth2 import service_account
|
|
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# Load credentials from environment variables
|
16 |
SERVICE_ACCOUNT_INFO = {
|
17 |
"type": "service_account",
|
@@ -145,10 +150,10 @@ def map_data_to_template(excel_file, mapping_file):
|
|
145 |
extracted_data = extracted_data[~extracted_data['SchDate'].isna()]
|
146 |
mapped_data = extracted_data.merge(mapping_data, on =['Part No.'],how='outer')[['Item Code','SchDate','Qty','Inventory Category']]
|
147 |
mapped_data = mapped_data[~mapped_data["SchDate"].isna()]
|
148 |
-
mapped_data = mapped_data[~mapped_data["SchDate"].str.strip().isin(["",None])]
|
|
|
149 |
mapped_data['SOType'] = "R"
|
150 |
-
|
151 |
-
return mapped_data
|
152 |
|
153 |
def save_mapping_file_to_drive():
|
154 |
# creds = Credentials.from_authorized_user_info(credentials_dict)
|
@@ -213,11 +218,51 @@ def pull_mapping_file_from_drive():
|
|
213 |
|
214 |
return 0
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
def main():
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
# File uploader
|
220 |
-
|
|
|
|
|
221 |
|
222 |
if uploaded_file is not None:
|
223 |
st.write("Uploaded PDF file:", uploaded_file.name)
|
@@ -228,10 +273,20 @@ def main():
|
|
228 |
file_present = pull_mapping_file_from_drive()
|
229 |
|
230 |
if file_present:
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
# Download link for the Excel file
|
237 |
# st.markdown(f"Download the extracted data in Excel file [here](/{excel_file})")
|
@@ -247,9 +302,10 @@ def main():
|
|
247 |
)
|
248 |
else:
|
249 |
st.error("Error: Converted Excel file not found")
|
250 |
-
|
|
|
251 |
if not file_present:
|
252 |
-
st.markdown("
|
253 |
mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
|
254 |
else:
|
255 |
mapping_data = pd.read_excel(MAPPING_FILENAME)
|
@@ -275,6 +331,7 @@ def main():
|
|
275 |
mapped_data = map_data_to_template(extracted_file, mapping_uploaded_file)
|
276 |
|
277 |
# Provide a link to download the final Excel file after mapping
|
|
|
278 |
st.markdown("### Final Excel File After Mapping")
|
279 |
|
280 |
final_excel_file = 'Final Data.xlsx'
|
|
|
11 |
from googleapiclient.discovery import build
|
12 |
from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
|
13 |
from google.oauth2 import service_account
|
14 |
+
import base64
|
15 |
|
16 |
+
def get_image_as_base64(image_path):
|
17 |
+
with open(image_path, "rb") as img_file:
|
18 |
+
return base64.b64encode(img_file.read()).decode()
|
19 |
+
|
20 |
# Load credentials from environment variables
|
21 |
SERVICE_ACCOUNT_INFO = {
|
22 |
"type": "service_account",
|
|
|
150 |
extracted_data = extracted_data[~extracted_data['SchDate'].isna()]
|
151 |
mapped_data = extracted_data.merge(mapping_data, on =['Part No.'],how='outer')[['Item Code','SchDate','Qty','Inventory Category']]
|
152 |
mapped_data = mapped_data[~mapped_data["SchDate"].isna()]
|
153 |
+
mapped_data = mapped_data[~mapped_data["SchDate"].str.strip().isin(["",None])]
|
154 |
+
|
155 |
mapped_data['SOType'] = "R"
|
156 |
+
return mapped_data[["SchDate","SOType","Item Code","Qty","Inventory Category"]]
|
|
|
157 |
|
158 |
def save_mapping_file_to_drive():
|
159 |
# creds = Credentials.from_authorized_user_info(credentials_dict)
|
|
|
218 |
|
219 |
return 0
|
220 |
|
221 |
+
def delete_master_file():
|
222 |
+
creds = authenticate()
|
223 |
+
service = build('drive', 'v3', credentials=creds)
|
224 |
+
# Authenticate with Google Drive API
|
225 |
+
# service = build('drive', 'v3', credentials=creds)
|
226 |
+
folder_id = "1HBRUZePST0D0buyU9MxeYg2vQyEL4wLF"
|
227 |
+
|
228 |
+
# List all files in the folder
|
229 |
+
results = service.files().list(
|
230 |
+
q=f"mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
|
231 |
+
fields="files(id, name)").execute()
|
232 |
+
files = results.get('files', [])
|
233 |
+
files = [i for i in files if i.get('name')==MAPPING_FILENAME]
|
234 |
+
|
235 |
+
if not files:
|
236 |
+
print('No Excel Mapping files found in the folder.')
|
237 |
+
else:
|
238 |
+
for file in files:
|
239 |
+
# Get the ID and name of the first Excel file found in the folder
|
240 |
+
existing_file_id = file['id']
|
241 |
+
existing_file_name = file['name']
|
242 |
+
|
243 |
+
# Delete the existing file
|
244 |
+
service.files().delete(fileId=existing_file_id).execute()
|
245 |
+
print("Deleted master file")
|
246 |
+
|
247 |
def main():
|
248 |
+
# Load your logo image
|
249 |
+
logo_path = "logo.jpeg"
|
250 |
+
logo_base64 = get_image_as_base64(logo_path)
|
251 |
+
logo_html = f"""
|
252 |
+
<div style="display: flex; justify-content: center; align-items: center; height: 100px;">
|
253 |
+
<img src="data:image/jpeg;base64,{logo_base64}" style="width: 100px; height: 100px;">
|
254 |
+
</div>
|
255 |
+
"""
|
256 |
+
|
257 |
+
# Display the logo HTML
|
258 |
+
st.markdown(logo_html, unsafe_allow_html=True)
|
259 |
+
st.markdown("<h1 style='text-align: center;'>PDF to Excel Converter</h1>", unsafe_allow_html=True)
|
260 |
+
|
261 |
|
262 |
# File uploader
|
263 |
+
st.markdown("### STEP 1")
|
264 |
+
st.markdown("#### Upload a PDF File")
|
265 |
+
uploaded_file = st.file_uploader("### Upload a PDF file", type=["pdf"])
|
266 |
|
267 |
if uploaded_file is not None:
|
268 |
st.write("Uploaded PDF file:", uploaded_file.name)
|
|
|
273 |
file_present = pull_mapping_file_from_drive()
|
274 |
|
275 |
if file_present:
|
276 |
+
try:
|
277 |
+
mapping_data_from_drive = pd.read_excel(MAPPING_FILENAME)
|
278 |
+
extracted_data_for_mapping = pd.read_excel(data_for_mapping)
|
279 |
+
extracted_data_for_mapping.columns = [i.strip() for i in extracted_data_for_mapping.columns]
|
280 |
+
mapping_data_from_drive.columns = [i.strip() for i in mapping_data_from_drive.columns]
|
281 |
+
|
282 |
+
extracted_data_for_mapping = extracted_data_for_mapping[['Customer Part no as per pdf','Customer Part name as per pdf','Customer Part color as per pdf',"Inventory Category as per pdf"]].merge(mapping_data_from_drive, on = ['Customer Part no as per pdf','Customer Part name as per pdf','Customer Part color as per pdf'], how='outer')
|
283 |
+
extracted_data_for_mapping.to_excel(data_for_mapping,index=False)
|
284 |
+
except Exception as e:
|
285 |
+
st.error("Error in the Mapping Master file on Cloud. " + str(e))
|
286 |
+
st.error("Please reupload the Data Master file with Item Code mapping")
|
287 |
+
delete_master_file()
|
288 |
+
file_present = None
|
289 |
+
|
290 |
|
291 |
# Download link for the Excel file
|
292 |
# st.markdown(f"Download the extracted data in Excel file [here](/{excel_file})")
|
|
|
302 |
)
|
303 |
else:
|
304 |
st.error("Error: Converted Excel file not found")
|
305 |
+
|
306 |
+
st.markdown("### STEP 2")
|
307 |
if not file_present:
|
308 |
+
st.markdown("#### Upload the Data Master file with Item Code mapping")
|
309 |
mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
|
310 |
else:
|
311 |
mapping_data = pd.read_excel(MAPPING_FILENAME)
|
|
|
331 |
mapped_data = map_data_to_template(extracted_file, mapping_uploaded_file)
|
332 |
|
333 |
# Provide a link to download the final Excel file after mapping
|
334 |
+
st.markdown("### FINAL DOWNLOAD")
|
335 |
st.markdown("### Final Excel File After Mapping")
|
336 |
|
337 |
final_excel_file = 'Final Data.xlsx'
|