ich bin gerade am Verzweifeln und hoffe mir kann Jemand helfen:
Im Prinzip habe ich eine input.ods Datei.
Und will, dass in der die erste Zeile (von oben) gesucht wird, wo der Eintrag in Spalte 7 ungleich "nein" ist.
Deren Einträge will ich später extern für was verwenden.
Dann soll in jener Zeile die erste Zelle mit dem aktuellen Datum überschrieben werden.
Und am Ende die ganze Tabelle nach der ersten Spalte (die Datume(?) enthalten) sortieren.
und speichern.
Nur scheitere ich brachial am Schritt wo die Liste sortiert werden soll.
Die Tabelle hat totsicher 7 Spalten, also müssten die Spaltenindizes von 0 bis 6 gehen.
Ich gebe als "Sortierspaltenindex" 0 vor.
Und der sagt mir konsequent, ich wäre ausserhalb der zulässigen index range!
Habe mir da von chatgpt alle möglichen varianten probieren lassen woran es liegen könnte, aber ich scheitere brachial :-/
Hier mal der komplette Code, aktuell sind noch zu debug zwecken Sachen drin wo zeilen oder so geprintet werden:
Code: Alles auswählen
import pyexcel_ods3 as pe
from pyexcel_ods3 import save_data
import os
import datetime
import time
def find_first_valid_row(sheet, invalid_value="nein"):
for row in sheet:
if row[6] != invalid_value:
return row
def print_sheet(sheet):
for row in sheet:
print(row)
def sort_and_save_ods_file(input_file, sort_column_index):
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)
sheet1 = file[sheet_name]
# Find the first valid row from the top where the 7th cell isn't "nein"
first_valid_row = find_first_valid_row(sheet1)
if not first_valid_row:
print("No valid row found.")
return
# Print the contents of the first valid row
print("First valid row contents:")
for cell in first_valid_row:
print(cell)
# Determine the legitimate values for sort_column_index
print("Legitimate values for sort_column_index:")
for idx, cell in enumerate(first_valid_row):
print(f"{idx}: {cell}")
# Update the first cell in the first column with the current date
current_date = datetime.datetime.now().strftime("%Y-%m-%d")
first_valid_row[0] = current_date
# Filter out empty rows from sheet1
sheet1 = [row for row in sheet1 if any(cell for cell in row)]
# Print the range of valid column indices after filtering
print("Valid column index range:", list(range(len(first_valid_row))))
# Check if sort_column_index is within the valid range
if sort_column_index not in range(len(first_valid_row)):
print("Invalid sort_column_index. Please choose a valid column index.")
return
time.sleep(1)
# Print the state of the sheet after sorting
print("State of the sheet before sorting:")
print_sheet(sheet1)
time.sleep(1)
# Decorate: Create a list of tuples with (value_in_specified_column, row_index, row_data)
decorated = [(row[sort_column_index], i, row) for i, row in enumerate(sheet1)]
# Sort based on the value in the specified column
decorated.sort()
# Undecorate: Extract the sorted rows based on the sorted indexes
sorted_sheet1 = [row_data for _, _, row_data in decorated]
# Print the state of the sheet after sorting
print("State of the sheet after sorting:")
print_sheet(sorted_sheet1)
# Save the sorted data to the output file
save_data(output_file, {sheet_name: sorted_sheet1})
# Remove the input file and rename the output file to the input file name
os.remove(input_file)
os.rename(output_file, input_file)
# Example usage:
if __name__ == "__main__":
input_file = "input.ods"
sort_column_index = 0 # Index of the column to sort (0-based index)
sort_and_save_ods_file(input_file, sort_column_index)
Und hier mal ein vereinfachtes Beispiel wie meine openoffice calc Tabelle aussieht:
Code: Alles auswählen
01.03.54 o 2 44 44 44 wer
04.05.62 z 5 88 88 88 nein
21.05.92 u 4 1 1 1 ja
31.01.19 r 7 9 9 9 vielleicht
03.03.19 e 8 34 34 34 ja
04.04.19 q 0 12 12 12 nein
06.05.19 w 9 56 56 56 nein
09.08.20 t 6 76 76 76 wer
02.03.23 i 3 23 23 23 vielleicht
09.12.30 p 1 62 62 62 nein
