Python Forum
New on python. Needs help with Google sheet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New on python. Needs help with Google sheet
#1
I'm new to coding.
I created the following code, but I cannot place the data entered in my Google Sheet file. JSON works.

The date must go in the next 2 free cells in line 3
The time will have to go in the 2 next free cells in line 4
The registration must go in the 2 next free cells in line 1
Flight number will have to go in the 2 next free cells in line 2
routing will have to go in the next 2 free cells in line 5

I would then need when I click Visualiser les commandes pour un jour précis for the program to access the Google sheet and find the registration, flight id, time, routing information. and this is exported to a pdf file.
I know it's probably a lot of code but I don't know how to do it anymore.
Starting the code has already taken me a long time, and I've been trying for several hours without success

See the google sheet attached
import tkinter as tk
from tkinter import messagebox, simpledialog
import gspread
from google.oauth2.service_account import Credentials
from datetime import datetime
import webbrowser

# Charger les informations d'identification depuis le fichier JSON
gc = gspread.service_account(filename=r"C:\Users\Jérôme\Downloads\XXXXXXXXXX.json")

# Ouvrir la feuille de calcul et l'onglet appropriés
wks = gc.open("XXXXX").worksheet("Service Order")

def save_order():
    compagnie_selected = selected_company.get()  
    date = date_entry.get()
    heure = heure_entry.get()
    immatriculation = immatriculation_entry.get()
    numero_vol = numero_vol_entry.get()
    routing = routing_entry.get()

    # Trouver les prochaines lignes vides
    next_empty_row = len(wks.get_all_values()) + 1

    # Insérer les données dans les prochaines lignes vides
    wks.update(f"A{next_empty_row}", immatriculation)
    wks.update(f"B{next_empty_row}", numero_vol)
    wks.update(f"C{next_empty_row}", date)
    wks.update(f"D{next_empty_row}", heure)
    wks.update(f"E{next_empty_row}", routing)
    wks.update(f"B{next_empty_row + 1}", numero_vol)

    # Afficher un message de confirmation
    messagebox.showinfo("Enregistrement", "La commande a été enregistrée avec succès !")

    # Fermer la fenêtre d'ajout de commande
    add_order_window.destroy()

def add_order(root):
    companies = ["xxxxxx", "xxxxxxxxx", "xxxxxxx", "xxxxxxxxxx", "xxxxxxxxx"]

    # Variable pour stocker la compagnie sélectionnée
    selected_company = tk.StringVar(root)
    selected_company.set(companies[0])  # Définir la première compagnie comme valeur par défaut

    add_order_window = tk.Toplevel(root)
    add_order_window.title("Ajouter une nouvelle commande")

    # Interface utilisateur pour saisir les détails de la commande
    tk.Label(add_order_window, text="Pour quelle compagnie est cette commande?").grid(row=0, column=0)
    compagnie_combobox = tk.OptionMenu(add_order_window, selected_company, *companies)
    compagnie_combobox.grid(row=0, column=1)

    tk.Label(add_order_window, text="Date de la commande (JJ/MM/AA):").grid(row=1, column=0)
    date_entry = tk.Entry(add_order_window)
    date_entry.grid(row=1, column=1)

    tk.Label(add_order_window, text="Heure du vol:").grid(row=2, column=0)
    heure_entry = tk.Entry(add_order_window)
    heure_entry.grid(row=2, column=1)

    tk.Label(add_order_window, text="Immatriculation avion:").grid(row=3, column=0)
    immatriculation_entry = tk.Entry(add_order_window)
    immatriculation_entry.grid(row=3, column=1)

    tk.Label(add_order_window, text="Numéro de vol:").grid(row=4, column=0)
    numero_vol_entry = tk.Entry(add_order_window)
    numero_vol_entry.grid(row=4, column=1)

    tk.Label(add_order_window, text="Routing (Départ - Destination):").grid(row=5, column=0)
    routing_entry = tk.Entry(add_order_window)
    routing_entry.grid(row=5, column=1)

    save_button = tk.Button(add_order_window, text="Enregistrer", command=save_order)
    save_button.grid(row=6, columnspan=2)

    # Bouton pour revenir à la fenêtre principale
    return_button = tk.Button(add_order_window, text="Retour", command=root.deiconify)
    return_button.grid(row=7, columnspan=2)

def main():
    root = tk.Tk()
    root.title("Gestionnaire de commandes")

    def open_consult_page():
        root.withdraw()  # Cache la fenêtre principale
        consult_orders()  # Ouvre la page de consultation des commandes
        root.deiconify()  # Affiche à nouveau la fenêtre principale quand la page de consultation est fermée

    def open_add_order_page():
        root.withdraw()
        add_order(root)  # Passer la variable root comme paramètre
        root.deiconify()

    tk.Button(root, text="Consulter les commandes pour un jour précis", command=open_consult_page).pack(pady=10)
    tk.Button(root, text="Ajouter une nouvelle commande", command=open_add_order_page).pack(pady=10)
    tk.Button(root, text="Quitter", command=root.quit).pack(pady=10)

    root.mainloop()

if __name__ == "__main__":
    main()

Attached Files

Thumbnail(s)
   
Reply
#2
I cannot find where you save your worksheet. Changing the worksheet does not modify the spreadsheet file.. There may be other problems, but start by fixing this so you can see what changes you are making.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,986 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,269 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,615 Jan-25-2022, 06:56 AM
Last Post: ndc85430
  install apache-airflow[postgres,google] on Python 3.8.12 virtual env ShahajaK 1 8,299 Oct-07-2021, 03:05 PM
Last Post: Larz60+
  Python script for excel sheet Nabil 4 3,335 Jun-01-2021, 05:09 AM
Last Post: Pedroski55
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,721 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Iterate through google sheet alcz 0 3,771 Aug-04-2020, 06:58 AM
Last Post: alcz
  Edit Open and Active Excel sheet in Python JoeDainton123 1 2,137 Jul-29-2020, 12:52 AM
Last Post: Larz60+
  how to import files in Google Collab wihout downloading them to PC and to Google Disk sveto4ka 9 3,985 Jun-03-2020, 12:40 PM
Last Post: sveto4ka
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,077 May-26-2020, 07:57 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020