Nur der Vollständigkeit halber mein finaler Code, zumindest was die ods.py Datei angeht:
Code: Alles auswählen
import datetime
import pyexcel_ods3 as pe
from pyexcel_ods3 import save_data
import os
import datetime
import time
import operator
import copy
def convert_to_date(obj):
# Check if the input is a string
if isinstance(obj, str):
# Convert the string to datetime.date format
#date_obj = datetime.datetime.strptime(obj, "%d.%m.%y").date()
date_obj = datetime.datetime.strptime(obj, "%d.%m.%Y").date()
return date_obj
elif isinstance(obj, datetime.date):
# If it's already a datetime.date object, return it as it is
return obj
else:
# print name and type
print(type(obj).__name__)
return obj
def convert_to_string(date_obj):
# Check if the input is a datetime.date object
if isinstance(date_obj, datetime.date):
# Convert the datetime.date object to a string in the format "27.09.1992"
formatted_string = date_obj.strftime("%d.%m.%Y")
return formatted_string
else:
# If it's not a datetime.date object, raise an error or handle accordingly.
raise ValueError("Invalid input. Expected a datetime.date object.")
def get_next_row():
input_file = "bewerbungzykler.ods"
output_file = "output.ods"
sheet_name = "Sheet1"
sort_ascending = True # Change to True for ascending order, False for descending order
# Read the data from the input file
file = pe.get_data(input_file)
sheet = file[sheet_name]
# Filter out empty rows from sheet1
sheet = [row for row in sheet if any(cell for cell in row)]
for row in sheet:
row[0]=convert_to_date(row[0])
replace_row_index=2
for i in range(len(sheet)):
if(sheet[i][6]!='nein'):
replace_row_index=i
break
#zu Testzwecken damit nicht immer die erste Zeile geändert wird
#replace_row_index=4
# Create a deep copy of the row before sorting and updating
original_row = copy.deepcopy(sheet[replace_row_index])
#print(sheet[replace_row_index])
# Get the current date as a datetime.date object
current_date = datetime.date.today()
#change value of the first cell in the replace_row_index th row
sheet[replace_row_index][0]=datetime.date.today()
# Convert date strings to datetime.date objects and get the rank order
dates = [convert_to_date(row[0]) for row in sheet]
rank_order = sorted(range(len(dates)), key=lambda x: dates[x])
# Sort the entire sheet based on the rank order
sorted_sheet = [sheet[i] for i in rank_order]
sheet=sorted_sheet
# Save the sorted data to the output file
save_data(output_file, {sheet_name: sheet})
# Remove the input file and rename the output file to the input file name
os.remove(input_file)
os.rename(output_file, input_file)
# Extract individual components from the original row before sorting and updating
original_date = convert_to_string(original_row[0])
components = original_row[1:] # Get all components except the first one
return convert_to_string(current_date), *components
#return original_row
Was es tut:
Man rugft von ausserhalb die get_next_row() funktion auf.
die guckt in die ods datei rein, sucht die erste zeile wo die 6te(?) zelle ungleich "nein" ist.
Und ändert die erste zelle in der zeile mit dem aktuellen datum ab und sortiert die datei noch aufsteigend nahc der 1. spalte.
die funktion gibt dann die abgeänderte zeile als lsite zurück, damit man extern mit den daten weiterarbeiten kann.
Mittlerweile bin ich am punkt, wo ich noch 2-3 weitere python dateien habe und mit der ausgelesenen zeile aus der ods datei ersetzungen in 2 odt dateien vornehme.
habe also 2 verschiedene "Template" odt dateien und ershcaffe damit neue odt dateien, wo bestimte begriffe ersetzt wurden.
was womit ersetzt wird, basiert uf der ausgelesenen zeile der obigen funktion.
und natürlich kann die funktion mehrfahc aufgerufen werden, sodass man der reihe nahc die daten der zeilen, von der ältesten der jüngsten, erhält.
nur wens interessiert, was ausser mir vermutlich niemand sein wird.
Ich hätte nun gerne die 2 erzeugten odt noch in pdfs umgewandelt, aber zumidnest laut chatgpt brauche ich entweder libreoffice (-.-) dazu oder eine der wenigen funktionierenden module hierfür erzeugt zwar pdfs, allerdings sit dann jegliche formatierung weg UND die bilder wurden von der odt nicht ins pdf übernommen.
also auch nicht das gewünschte.
Da hängts nun aktuell dran, was brauchbares zum umwandeln zu finden
