Louis Chabert
Louis Chabert

Reputation: 439

How apply changes on ods file using the library ezodf in python?

I have 2 .ods files :

Do you have any ideas ?

Here is my code :

def increment_counter(app_name, data):
app_exists = any(row['Application'] == app_name for row in data)

if app_exists:
    for row in data:
        if row['Application'] == app_name:
            row['Counter'] += 1
else:
    new_row = {'Application': app_name, 'Version': '', 'Category': '',
               'Encryption': '', 'Interest': '', 'Comments': '', 'Counter': 1}
    data.append(new_row)


def update_counter(source_file, destination_file, source_sheet, destination_sheet):
source_data = pe.get_records(file_name=source_file, sheet_name=source_sheet)
destination_data = pe.get_records(file_name=destination_file, sheet_name=destination_sheet)

for row in source_data:
    app_name = row['Application']
    increment_counter(app_name, destination_data)

destination_book = ezodf.opendoc(destination_file)

destination_sheet_index = -1
for i, sheet in enumerate(destination_book.sheets):
    if sheet.name == destination_sheet:
        destination_sheet_index = i
        break

destination_book.sheets[destination_sheet_index].data = destination_data

destination_book.saveas(destination_file)
bak_file = f"{destination_file}.bak"
if os.path.exists(bak_file):
    os.remove(bak_file)


source_file = 'UnsupportedApp.ods'
source_sheet = 'appFromAppStore_Result'
destination_sheet = 'appFromAppStore'
update_counter(source_file, filePathAppAndroid, source_sheet, destination_sheet)

Upvotes: 2

Views: 185

Answers (0)

Related Questions