Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import xlrd, xlwt
  2. import os
  3. import re
  4.  
  5. get_year = '2017'
  6.  
  7. #вычищаем из строки все символы кроме цифр
  8. def clean_date(value):
  9. value = re.sub(r'\D', "", value)
  10. return value
  11.  
  12. #очищаю список файлов от шлака
  13. def clean_list_dir(list_dir):
  14. index = len(list_dir) - 1
  15. while index >= 0:
  16. if "Консолидированный" not in list_dir[index]:
  17. # print("Удаляем из списка лишний файл: ", list_dir[index])
  18. del list_dir[index]
  19. index -= 1
  20.  
  21. # очищаю список файлов от дублей
  22. def clean_duble_list_dir(list_dir):
  23. max_len_file = 0
  24. max_i = 0
  25. index = len(list_dir) - 1
  26. for i in range(0, len(list_dir)):
  27. if len(list_dir[i]) > max_len_file:
  28. max_len_file = len(list_dir[i])
  29. max_i = i
  30. name = str(list_dir[max_i])
  31. print("-"*10, name)
  32. while index >= 0:
  33. if str(list_dir[index]) != name and str(list_dir[index])[:28] in name:
  34. print(" Удаляем из списка дубль: ", list_dir[index])
  35. del list_dir[index]
  36. index -= 1
  37.  
  38. #Получаю данные из списка файлов в директории
  39. def get_spis_data(cwd, list_dir):
  40. for file in list_dir:
  41. excel_data_file = xlrd.open_workbook(str(cwd) + "\\" + str(file), encoding_override='ISO-8859-1')
  42. #добавила кодировку для файлов от 2017 года и старше
  43. sheet = excel_data_file.sheet_by_index(0)
  44. row_num = sheet.nrows
  45. spis_data_file = []
  46. for i in range(6, 12):
  47. spis_data_file.append(sheet.row(row_num - 1)[i].value)
  48. # print(file)
  49. spis_data[clean_date(sheet.row(6)[6].value)] = spis_data_file
  50. # print("---------- длина списка после добавления нового месяца -----------")
  51. # print(len(spis_data))
  52.  
  53. def what_the_mounth(n):
  54. if n == "01":
  55. mm = "Январь"
  56. elif n == "02":
  57. mm = "Февраль"
  58. elif n == "03":
  59. mm = "Март"
  60. elif n == "04":
  61. mm = "Апрель"
  62. elif n == "05":
  63. mm = "Май"
  64. elif n == "06":
  65. mm = "Июнь"
  66. elif n == "07":
  67. mm = "Июль"
  68. elif n == "08":
  69. mm = "Август"
  70. elif n == "09":
  71. mm = "Сентябрь"
  72. elif n == "10":
  73. mm = "Октябрь"
  74. elif n == "11":
  75. mm = "Ноябрь"
  76. elif n == "12":
  77. mm = "Декабрь"
  78. return mm
  79.  
  80. #собираю список директорий
  81. if get_year == "2019":
  82. get_dir = "T:\\0 АРХИВ\\"
  83. else:
  84. get_dir = "T:\\0 АРХИВ\\"+get_year
  85. os.chdir(get_dir)
  86. list_path = os.listdir('.')
  87.  
  88. index = len(list_path) - 1
  89. while index >= 0:
  90. if get_year not in list_path[index]:
  91. del list_path[index]
  92. index -= 1
  93.  
  94. print(get_dir)
  95. print(list_path)
  96. spis_data = {}
  97. for path in list_path:
  98. #собираю список файлов в директории
  99. file_in_path = get_dir + "\\" + str(path)
  100. os.chdir(file_in_path)
  101. list_dir = os.listdir('.') #список файлов
  102. cwd = os.getcwd() #директория
  103.  
  104. #очищаю список файлов от шлака и дублей
  105. clean_list_dir(list_dir)
  106. clean_duble_list_dir(list_dir)
  107. #Получаю данные из списка файлов в директории
  108. get_spis_data(cwd, list_dir)
  109.  
  110. print(len(spis_data))
  111. print(spis_data)
  112.  
  113. new_book = xlwt.Workbook()
  114. new_book_sheet = new_book.add_sheet('Итоги консолидированных')
  115.  
  116. new_book_sheet.write(0, 0, 'Год')
  117. new_book_sheet.write(0, 1, 'Месяц')
  118. new_book_sheet.write(0, 2, 'День')
  119. new_book_sheet.write(0, 3, 'В работе')
  120. new_book_sheet.write(0, 4, 'Сумма задолженности просроченных договоров созданных')
  121. new_book_sheet.write(1, 4, 'до 21 дня')
  122. new_book_sheet.write(1, 5, '% не возврата')
  123. new_book_sheet.write(1, 6, 'от 21 дня')
  124. new_book_sheet.write(0, 7, '% не возврата от 21 дня')
  125. new_book_sheet.write(0, 8, 'Изменение долга от 21 дн')
  126. i = 2 # строка, с которой начинаем записывать полученные значения
  127.  
  128. for key, value in spis_data.items():
  129. year = "20" + str(key[4:])
  130. mm = str(key[2:4])#what_the_mounth(str(key[2:4]))
  131. day = str(key[:2])
  132.  
  133. new_book_sheet.write(i, 0, str(year))
  134. new_book_sheet.write(i, 1, mm)
  135. new_book_sheet.write(i, 2, day)
  136.  
  137. for n in range(0, len(value)):
  138. new_book_sheet.write(i, n+3, value[n])
  139.  
  140. print(year + " " + mm + " " + day, value)
  141. print('Записана строка: ', i)
  142. i += 1
  143.  
  144. new_book.save('D:/new_book_'+get_year+'.xls')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement