Мой код python работает с тем, чтобы принимать данные из CSV-файла и преобразовывать его в файл .ods. Проблема в том, что при сохранении нового файла он удаляет другие листы в файле, который он сохраняет. Как получить его, чтобы обновить новый файл, не удаляя другие листы?Как создать/заполнить лист в файле ods, не уничтожая другие листы в файле?
import csv, shutil, os, glob
from pyexcel_ods3 import save_data
from collections import OrderedDict
# finds the latest file from my downloads folder
newest = max(glob.iglob('*.csv'), key=os.path.getctime)
my_file = open(newest)
file_reader = csv.reader(my_file)
file_data = list(file_reader)
# identifies the data structure
file_header = ['Details', 'Posting Date', 'Description', 'Amount', 'Type', 'Balance', 'Check or Slip #']
# compares the data structure in the csv file to the one used for this script
if file_data[0] == file_header:
del file_data[0] # removes the header form the csv file
date = file_data[0][1]
f_date = date.split('/')
f_date_formatted = '{}-{}-{}'.format(f_date[2], f_date[0], f_date[1])
for i in file_data: # organizes the data
i[4] = ''
transaction = float(i[3])
if transaction > 0:
i[3] = ''
i[4] = transaction
my_file.close()
# creates a copy of the unpopulated file. This file has 3 existing sheets. One sheet is called 'DATA'
shutil.copy('Checkbook Monthly balance.ods', 'Checkbook Monthly balance thru {}.ods'.format(f_date_formatted))
# populates the sheet called 'DATA'
data = OrderedDict()
data.update({'DATA': file_data})
save_data('Checkbook Monthly balance thru {}.ods'.format(f_date_formatted), data)
else:
print('It appears the bank changed the table format. Check the .csv file for a difference in the data structure.')
my_file.close()
Я ценю ваш ответ. Я довольно новичок в Python и борюсь с тем, как отформатировать это. Я понимаю, что вы говорите. Мне просто нужна помощь в формате. – draugr
get_data («Checkbook Monthly balance.ods») вернет словарь: {'DATA': file_data}. – chfw
ОК. Я смог сделать то, что хочу, и сохранить другие листы, однако удаляет все форматирование с других листов. Любой способ сохранить форматирование? – draugr