from dataclasses import dataclass import gspread import csv import locale import datetime import yaml import os from pathlib import Path #TODO: Upload automatically! @dataclass class Export: # all necessary fields for ELSAP-importfiles sum_col: str desc_col: str def export_timesheet(csvexport_filename, ex: Export): if not hasattr(export_timesheet, "row"): export_timesheet.row = 4 with open(f"{csvexport_filename}.csv", 'w') as file: writer = csv.writer(file, delimiter=';', lineterminator="\n") period = datetime.datetime.strptime(cfg['worksheet_name'], "%B %Y").date().replace(day=1) writer.writerow(["","","","Current Period",f"{period.strftime('%d.%m.%Y')}"]) writer.writerow("") writer.writerow(["Name","PersNr","CostCenter","Day","Date","Status","WBS element","Description","PARMA","Task Name","Activity Code","ActType","Profit center","Quantity","Unit","Project description","Customer name","Statistical key figure","A/AType","Description A/A type","AI","Activity","Short text"]) # iterate for time_entry in list_of_dicts: date_str_de_de = time_entry.get("date") if date_str_de_de not in ["","Wochensumme","Summe (kumuliert)"]: datetime_object = datetime.datetime.strptime(date_str_de_de, '%a., %d. %B %Y') writer.writerow([cfg['name'],cfg['pers_nr'],"",f"=WEEKDAY(E{export_timesheet.row})",datetime_object.strftime('%d.%m.%Y'),"",cfg['WBS_element'],cfg['WBS_desc'],"","","","","",time_entry.get(ex.sum_col),"Hours",time_entry.get(ex.desc_col)]) export_timesheet.row += 1 # Check if the file size is small (indicating only the header) file_path = f"{csvexport_filename}.csv" # Use os.path.getsize to get the file size if os.path.getsize(file_path) <= 190: # Adjust this size threshold based on your expected header size os.remove(file_path) print(f"File {file_path} was deleted because it only contained the header.") else: print(f"File {file_path} contains data and was not deleted.") # Set locale to German (Germany) locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8') #locale.setlocale(locale.LC_ALL, 'de_AT.UTF-8') with open("./app/config.yaml", encoding='utf8') as f: cfg = yaml.load(f, Loader=yaml.FullLoader) home = Path.home() gc = gspread.service_account(home / "service_account.json") sh = gc.open(cfg['spreadsheet_name']) worksheet = sh.worksheet(cfg['worksheet_name']) week = cfg['week'] # 10, 11, 12 or None username = cfg['username'] name = cfg['name'] ansprechpartner_username = cfg['ansprechpartner_username'] ansprechpartner_name = cfg['ansprechpartner_name'] # read the whole sheet list_of_dicts = worksheet.get_all_records(numericise_ignore=['all']) for key in ["gems"]: # extract period out of sheet-name period = datetime.datetime.strptime(cfg['worksheet_name'], "%B %Y").date().replace(day=1) csvexport_filename = f"GEMS_{period.strftime('%Y-%m')} LNW {cfg['name']}" ex1 = Export(sum_col=f"sum_{key}", desc_col=f"desc_{key}") export_timesheet(csvexport_filename=csvexport_filename, ex=ex1)