Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xlrd, xlwt
- import os
- import re
- get_year = '2017'
- #вычищаем из строки все символы кроме цифр
- def clean_date(value):
- value = re.sub(r'\D', "", value)
- return value
- #очищаю список файлов от шлака
- def clean_list_dir(list_dir):
- index = len(list_dir) - 1
- while index >= 0:
- if "Консолидированный" not in list_dir[index]:
- # print("Удаляем из списка лишний файл: ", list_dir[index])
- del list_dir[index]
- index -= 1
- # очищаю список файлов от дублей
- def clean_duble_list_dir(list_dir):
- max_len_file = 0
- max_i = 0
- index = len(list_dir) - 1
- for i in range(0, len(list_dir)):
- if len(list_dir[i]) > max_len_file:
- max_len_file = len(list_dir[i])
- max_i = i
- name = str(list_dir[max_i])
- print("-"*10, name)
- while index >= 0:
- if str(list_dir[index]) != name and str(list_dir[index])[:28] in name:
- print(" Удаляем из списка дубль: ", list_dir[index])
- del list_dir[index]
- index -= 1
- #Получаю данные из списка файлов в директории
- def get_spis_data(cwd, list_dir):
- for file in list_dir:
- excel_data_file = xlrd.open_workbook(str(cwd) + "\\" + str(file), encoding_override='ISO-8859-1')
- #добавила кодировку для файлов от 2017 года и старше
- sheet = excel_data_file.sheet_by_index(0)
- row_num = sheet.nrows
- spis_data_file = []
- for i in range(6, 12):
- spis_data_file.append(sheet.row(row_num - 1)[i].value)
- # print(file)
- spis_data[clean_date(sheet.row(6)[6].value)] = spis_data_file
- # print("---------- длина списка после добавления нового месяца -----------")
- # print(len(spis_data))
- def what_the_mounth(n):
- if n == "01":
- mm = "Январь"
- elif n == "02":
- mm = "Февраль"
- elif n == "03":
- mm = "Март"
- elif n == "04":
- mm = "Апрель"
- elif n == "05":
- mm = "Май"
- elif n == "06":
- mm = "Июнь"
- elif n == "07":
- mm = "Июль"
- elif n == "08":
- mm = "Август"
- elif n == "09":
- mm = "Сентябрь"
- elif n == "10":
- mm = "Октябрь"
- elif n == "11":
- mm = "Ноябрь"
- elif n == "12":
- mm = "Декабрь"
- return mm
- #собираю список директорий
- if get_year == "2019":
- get_dir = "T:\\0 АРХИВ\\"
- else:
- get_dir = "T:\\0 АРХИВ\\"+get_year
- os.chdir(get_dir)
- list_path = os.listdir('.')
- index = len(list_path) - 1
- while index >= 0:
- if get_year not in list_path[index]:
- del list_path[index]
- index -= 1
- print(get_dir)
- print(list_path)
- spis_data = {}
- for path in list_path:
- #собираю список файлов в директории
- file_in_path = get_dir + "\\" + str(path)
- os.chdir(file_in_path)
- list_dir = os.listdir('.') #список файлов
- cwd = os.getcwd() #директория
- #очищаю список файлов от шлака и дублей
- clean_list_dir(list_dir)
- clean_duble_list_dir(list_dir)
- #Получаю данные из списка файлов в директории
- get_spis_data(cwd, list_dir)
- print(len(spis_data))
- print(spis_data)
- new_book = xlwt.Workbook()
- new_book_sheet = new_book.add_sheet('Итоги консолидированных')
- new_book_sheet.write(0, 0, 'Год')
- new_book_sheet.write(0, 1, 'Месяц')
- new_book_sheet.write(0, 2, 'День')
- new_book_sheet.write(0, 3, 'В работе')
- new_book_sheet.write(0, 4, 'Сумма задолженности просроченных договоров созданных')
- new_book_sheet.write(1, 4, 'до 21 дня')
- new_book_sheet.write(1, 5, '% не возврата')
- new_book_sheet.write(1, 6, 'от 21 дня')
- new_book_sheet.write(0, 7, '% не возврата от 21 дня')
- new_book_sheet.write(0, 8, 'Изменение долга от 21 дн')
- i = 2 # строка, с которой начинаем записывать полученные значения
- for key, value in spis_data.items():
- year = "20" + str(key[4:])
- mm = str(key[2:4])#what_the_mounth(str(key[2:4]))
- day = str(key[:2])
- new_book_sheet.write(i, 0, str(year))
- new_book_sheet.write(i, 1, mm)
- new_book_sheet.write(i, 2, day)
- for n in range(0, len(value)):
- new_book_sheet.write(i, n+3, value[n])
- print(year + " " + mm + " " + day, value)
- print('Записана строка: ', i)
- i += 1
- new_book.save('D:/new_book_'+get_year+'.xls')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement