File size: 3,751 Bytes
41eac88 08268ab 41eac88 08268ab 41eac88 17f885a 41eac88 08268ab 41eac88 17f885a 08268ab 41eac88 17f885a 41eac88 08268ab 17f885a 08268ab 17f885a 08268ab 17f885a 08268ab 17f885a 3e6cc4a 41eac88 17f885a 08268ab 17f885a 08268ab 17f885a 08268ab 17f885a 41eac88 17f885a 41eac88 17f885a 41eac88 17f885a 41eac88 17f885a 08268ab 41eac88 08268ab 41eac88 17f885a 08268ab 17f885a 08268ab 41eac88 08268ab 41eac88 08268ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import gradio as gr
import pandas as pd
import time
import gspread
from google.oauth2.service_account import Credentials
from datetime import datetime, timedelta
import pytz
# Global variable to control process interruption
stop_flag = False
# Authenticate using the service account credentials
def authenticate_google_sheets():
print("Authenticating with Google Sheets...")
scope = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive"
]
creds = Credentials.from_service_account_file('credentials.json', scopes=scope)
client = gspread.authorize(creds)
print("Authentication successful.")
return client
# Main function to upload Excel data to Google Sheets
def upload_to_google_sheets(excel_file, spreadsheet_id, delay):
global stop_flag
stop_flag = False # Reset stop flag when process starts
logs = [] # To store logs for each row
try:
client = authenticate_google_sheets()
sheet = client.open_by_key(spreadsheet_id).sheet1
logs.append(f"Connected to Google Sheet: {spreadsheet_id}")
except Exception as e:
logs.append(f"Error connecting to Google Sheets: {e}")
return "\n".join(logs)
try:
df = pd.read_excel(excel_file.name)
logs.append("Excel file read successfully.")
except Exception as e:
logs.append(f"Error reading the Excel file: {e}")
return "\n".join(logs)
#df = df.replace([float('inf'), float('-inf')], None).where(pd.notnull(df), None)
for index, row in df.iterrows():
if stop_flag:
logs.append("Process interrupted by the user.")
return "\n".join(logs)
try:
row_data = row.tolist()
sheet.append_row(row_data)
logs.append(f"Row {index} uploaded successfully: {row_data}")
except Exception as e:
logs.append(f"Error processing row {index}: {row_data}, Error: {e}")
time.sleep(delay)
logs.append("All rows processed.")
return "\n".join(logs)
# Stop the process
def stop_process():
global stop_flag
stop_flag = True
return "Process will stop after the current row."
# Schedule upload with delay and target time
def schedule_upload(excel_file, spreadsheet_id, delay, target_time):
pacific = pytz.timezone("America/Los_Angeles")
current_time = datetime.now(pacific)
target_time = pacific.localize(datetime.strptime(target_time, '%Y-%m-%d %H:%M:%S'))
time_difference = (target_time - current_time).total_seconds()
if time_difference > 0:
time.sleep(time_difference)
return upload_to_google_sheets(excel_file, spreadsheet_id, delay)
# Gradio Interface
def gradio_interface(excel_file, spreadsheet_id, delay, target_time):
return schedule_upload(excel_file, spreadsheet_id, int(delay), target_time)
# Gradio app
with gr.Blocks() as demo:
excel_file_input = gr.File(label="Upload Excel File")
spreadsheet_id_input = gr.Textbox(label="Google Spreadsheet ID")
delay_input = gr.Number(label="Delay between rows (in seconds)", value=300)
target_time_input = gr.Textbox(label="Start time (Pacific Time, format: YYYY-MM-DD HH:MM:SS)", placeholder="2024-09-25 14:30:00")
result_output = gr.Textbox(label="Result")
upload_button = gr.Button("Schedule Upload")
stop_button = gr.Button("Stop Process")
stop_status_output = gr.Textbox(label="Status")
upload_button.click(fn=gradio_interface,
inputs=[excel_file_input, spreadsheet_id_input, delay_input, target_time_input],
outputs=result_output)
stop_button.click(fn=stop_process, inputs=[], outputs=stop_status_output)
demo.launch()
|