import csv from datetime import datetime import collections import os.path from google.oauth2 import service_account from googleapiclient.discovery import build # discovery wird für das resource-Objekt der Lib benötigt from googleapiclient.errors import HttpError # Importiere die neue Bibliothek - KORRIGIERTER IMPORT und KLASSENNAME from gdoctableapppy.gdoctableapp import gdoctableapp # Klasse ist klein geschrieben # --- Konfiguration --- CSV_FILENAME = 'Namensliste.csv' GOOGLE_DOC_TITLE = f"Gruppenlisten_{datetime.now().strftime('%Y-%m-%d_%H-%M')}" TARGET_FOLDER_ID = "18DNQaH9zbcBzwhckJI-4Uah-WXTXg6bg" # Diese werden nur für den einmaligen Infoblock verwendet EINRICHTUNG_INFO = "Kinderhaus St. Martin Neuching" FOTODATUM_INFO = "02. - 05.06.2025" GRUPPENNAME_SUFFIX = "gruppe" SCOPES = [ 'https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive.file' ] SERVICE_ACCOUNT_FILE = 'service_account.json' # --- Ende Konfiguration --- def get_services_with_service_account(): creds = None; docs_service = None; drive_service = None try: creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) except Exception as e: print(f"Fehler Credentials: {e}"); return None, None try: docs_service = build('docs', 'v1', credentials=creds); print("Docs API Service erstellt.") except Exception as e: print(f"Fehler Docs Service: {e}") try: if any(s in SCOPES for s in ['https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive']): drive_service = build('drive', 'v3', credentials=creds); print("Drive API Service erstellt.") else: print("WARNUNG: Kein Drive Scope für Drive Service.") except Exception as e: print(f"Fehler Drive Service: {e}") return docs_service, drive_service Info: Ordner-ID für Dokument: '18DNQaH9zbcBzwhckJI-4Uah-WXTXg6bg' Docs API Service erstellt. Drive API Service erstellt. Neues leeres Doc via Drive API in Ordner '18DNQaH9zbcBzwhckJI-4Uah-WXTXg6bg' erstellt, ID: 190HPlsHFI-QcwVoKav6BUcgy3WOeg4Kp9WkFik1El5I Einmalige Info eingefügt. Versuche Tabelle für Gruppe 'Bienen' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Bienen': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Bienen: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Eulen' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Eulen': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Eulen: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Frösche' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Frösche': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Frösche: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Hasen' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Hasen': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Hasen: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Kobolde' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Kobolde': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Kobolde: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Marienkäfer' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Marienkäfer': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Marienkäfer: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Maulwürfe' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Maulwürfe': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Maulwürfe: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Schmetterlinge' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Schmetterlinge': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Schmetterlinge: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Wichtel' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Wichtel': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Wichtel: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Wölfe' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Wölfe': 'gdoctableapp' object has no attribute 'add_table' Fehler beim Einfügen des PageBreak nach Wölfe: 'gdoctableapp' object has no attribute 'add_page_break' Versuche Tabelle für Gruppe 'Zwergerl' mit gdoctableapp einzufügen... Fehler bei Tabelle/Footer für Gruppe 'Zwergerl': 'gdoctableapp' object has no attribute 'add_table' Sende zusätzliche Fallback-Requests für Doc ID '190HPlsHFI-QcwVoKav6BUcgy3WOeg4Kp9WkFik1El5I'... --- SKRIPT BEENDET --- Dokument-ID: 190HPlsHFI-QcwVoKav6BUcgy3WOeg4Kp9WkFik1El5I Link: https://docs.google.com/document/d/190HPlsHFI-QcwVoKav6BUcgy3WOeg4Kp9WkFik1El5I/edit HINWEIS: Es wurde versucht, echte Tabellen mit fetter Kopfzeile via gdoctableapppy zu erstellen. root@Diskstation2:/volume1/homes/Floke/python/brancheneinstufung# # --- Main execution block --- # (Bleibt weitgehend gleich, ruft generate_tables_with_gdoctableapp auf) if __name__ == "__main__": print(f"Info: Ordner-ID für Dokument: '{TARGET_FOLDER_ID}'") # Wichtig: discovery.build wird für das resource Objekt der Lib gebraucht, # unser get_services_with_service_account gibt aber das schon gebaute service Objekt zurück. # Wir sollten `creds` zurückgeben und den Service ggf. neu bauen oder das `resource` Objekt anpassen. # Einfacher: `docs_api_service` ist das, was die Lib als `docsService` braucht. docs_api_service, drive_api_service = get_services_with_service_account() if not docs_api_service: print("Konnte Google Docs API Service nicht initialisieren. Skript wird beendet.") else: document_id = None # 1. Dokument erstellen (wie zuvor) # ... (Code für Dokumenterstellung bleibt gleich) ... if drive_api_service and TARGET_FOLDER_ID: # ... (Drive API Erstellung) ... file_metadata = {'name': GOOGLE_DOC_TITLE, 'mimeType': 'application/vnd.google-apps.document', 'parents': [TARGET_FOLDER_ID]} try: created_file = drive_api_service.files().create(body=file_metadata, fields='id').execute() document_id = created_file.get('id') print(f"Neues leeres Doc via Drive API in Ordner '{TARGET_FOLDER_ID}' erstellt, ID: {document_id}") except Exception as e: print(f"Fehler Drive API Erstellung: {e}\n Versuche Fallback...") if not document_id: try: doc = docs_api_service.documents().create(body={'title': GOOGLE_DOC_TITLE}).execute() document_id = doc.get('documentId') print(f"Neues leeres Doc via Docs API (Root SA) erstellt, ID: {document_id}") if TARGET_FOLDER_ID: print(f" BITTE manuell in Ordner '{TARGET_FOLDER_ID}' verschieben.") except Exception as e: print(f"Konnte leeres Doc auch nicht mit Docs API erstellen: {e}") if document_id: # 2. Einmalige Info am Anfang # ... (Code für initial_info bleibt gleich, wird mit docs_api_service.batchUpdate eingefügt) ... initial_info_lines = ["Info zum Kopieren für Ihre manuelle Kopfzeile:", EINRICHTUNG_INFO, FOTODATUM_INFO, "\n" + "="*70 + "\n" ] initial_text = "\n".join(initial_info_lines) + "\n"; initial_requests = [{'insertText': {'location': {'index': 1}, 'text': initial_text}}] try: docs_api_service.documents().batchUpdate(documentId=document_id, body={'requests': initial_requests}).execute(); print("Einmalige Info eingefügt.") except HttpError as err: print(f"Fehler bei einmaliger Info: {err}") # 3. Dokument mit Tabellen befüllen success_filling = generate_tables_with_gdoctableapp(docs_api_service, document_id) if success_filling: print(f"\n--- SKRIPT BEENDET ---"); print(f"Dokument-ID: {document_id}"); print(f"Link: https://docs.google.com/document/d/{document_id}/edit") print("HINWEIS: Es wurde versucht, echte Tabellen mit fetter Kopfzeile via gdoctableapppy zu erstellen.") else: print("\n--- FEHLER BEIM BEFÜLLEN MIT TABELLEN ---") else: print("\n--- FEHLGESCHLAGEN: Kein Dokument erstellt ---")