K
K
kiddle2017-10-29 21:13:48
Python
kiddle, 2017-10-29 21:13:48

How to save a file when clicking the cross of the application window?

Hello, I am making a parser. All information is stored in an excel file. There is one thing, but if the parser is urgently terminated, then often the data is not saved and the excel file is then damaged. How to make it so that the program exited only after saving? or is there some other way?

if not os.path.exists('output/output.xlsx'):
    wb = Workbook().save('output/output.xlsx')
else:
    wb = load_workbook('output/output.xlsx')

def write_to_excel(data, pos, name):
    ws = wb[name]
    ws['A' + pos] = int(pos) - 1
    ws['B' + pos] = data.get('Полное наименование/ФИО ИП', '')
    ws['C' + pos] = data.get('Адрес места нахождения ЮЛ/ИнЮЛ или жительства ИП', '')
    ws['D' + pos] = data.get('ОГРН', '')
    ws['E' + pos] = data.get('В лице', '')
    ws['F' + pos] = data.get('ФИО руководителя', '')
    ws['G' + pos] = data.get('Номер телефона', '')
    ws['H' + pos] = data.get('Адрес электронной почты', '')
    ws['I' + pos] = data.get('Регистрационный номер', '')
    ws['J' + pos] = data.get('Дата начала действия', '')
    ws['K' + pos] = data.get('Дата окончания действия', '')
    ws['L' + pos] = data.get('Схема декларирования', '')
    ws['M' + pos] = data.get('Статус', '')
    ws['N' + pos] = data.get('Стандарты', '')
    ws['O' + pos] = data.get('ТН ВЭД', '') if data.get('ТН ВЭД', '') else data.get('ТН ВЭД ТС', '')
    wb.save('output/output.xlsx')

for link in open('...'):
    try:
        .....
        write_to_excel(data, str(pos), sheetname)
     finally:
         wb.save('output.xlsx')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question