Reputation: 439
I have 2 .ods files :
The first is called AppAndroid.ods. This has the heading Application No. Version Category Encryption Interest Comments Counter Each column is separated by a space but in my file this corresponds to different columns. This file lists all previously encountered applications with the name, version number, etc. and the counter column which counts the number of times the application has been encountered. This ods file has a sheet called appFromAppStore and an Others sheet. The heading is the same on both sheets.
The second ods file is called UnsupportedApp.ods, this one is very similar to the first. Here is its header: Application No. Version Category Encryption Interest Comments In this case, there is no counter column because this file changes every time my python program is executed. This ods file has a sheet called appFromAppStore_Result and an Others_Result sheet. The heading is the same on both sheets.
What does my python program do ? Currently this copies the information from my UnsupportedApp.ods file into AppAndroid.ods. The latter lists all previously encountered applications. That said, UnsupportedApp.ods is ephemeral and case-specific. I made a function that allows me to add +1 to my counter when the application present in UnsupportedApp.ods is present in AppAndroid.ods. I want to update the latter however I cannot do it, I display the information in my console step by step, my counters increment well but when the recording is done and I open my document. ods thereafter it did not move.
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