Advertisement
Danila_lipatov

MERGE_FILE_EXCEL

Aug 8th, 2022 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import openpyxl as xl
  4. import os
  5. import natsort
  6. import glob as gb
  7. from openpyxl import Workbook
  8. import filecmp
  9. import time
  10.  
  11.  
  12. os.getcwd()
  13.  
  14. file_paths_1 = {}    #RS2.8
  15.  
  16. file_paths_2 = {}     #RS2.7
  17.  
  18. check_mass = []
  19.  
  20. #sort_nass = natsort.natsorted(nass_1, reverse=False)
  21. for dirpath, dirnames, filenames in os.walk("."):
  22.     for filename in filenames:
  23.         if filename.endswith('2.8.xlsm'):
  24.           if filename not in file_paths_1:
  25.             file_paths_1[filename] = []
  26.           file_paths_1[filename].append('./' + dirpath.partition('\\')[2] +'/')
  27.  
  28.  
  29. for dirpath, dirnames, filenames in os.walk("."):
  30.     for filename in filenames:
  31.         if filename.endswith('2.7.xlsm'):
  32.           if filename not in file_paths_2:
  33.             file_paths_2[filename] = []
  34.           file_paths_2[filename].append('./' + dirpath.partition('\\')[2] +'/')
  35.  
  36. for file1, path1 in file_paths_1.items():
  37.         check_mass.append(file1.partition('_')[0])
  38. print(set(check_mass))
  39.  
  40. print(len(set(check_mass)))
  41.  
  42. count = 0
  43. for fil2, path2 in file_paths_2.items():
  44.     if fil2.partition('_')[0] in check_mass:
  45.         print("YES")
  46.     else:
  47.         print("NO", fil2)
  48.         count += 1
  49. print(count)
  50.  
  51.  
  52. for file, path in file_paths_1.items():
  53.             df_main = pd.read_excel(path[0] + file, sheet_name='Лист1')
  54.             df_main = df_main.drop(columns=df_main.iloc[:, 1:43])
  55.             k = 1
  56.             for p in path:
  57.                 df_temp = pd.read_excel(p + file, sheet_name='Лист1')
  58.                 df_temp = df_temp.iloc[:, 1]
  59.                 df_main = pd.concat([df_main, df_temp], axis=1)
  60.                 # print(k, p, file)
  61.  
  62.                 k += 1
  63.             for file2, path2 in file_paths_2.items():
  64.                 # print(path[0] + file)
  65.                 # print(len(path))
  66.                 k = 1
  67.                 # print(file, file2, p2, i)
  68.                 # i += 1
  69.                 if file.partition('_')[0] == file2.partition('_')[0]:  # TODO if file != file2 ===>>>>>    upgrade__
  70.                     # ___new df_main(path2[0] + file2, .....)___
  71.                     # ___Save files (f'output{file}.xlsx')
  72.                     for p2 in path2:
  73.                         df_temp = pd.read_excel(p2 + file2, sheet_name='Лист1')
  74.                         if p2 != './05.2020-10.2021 2.7 new/':
  75.                             df_temp = df_temp.iloc[:, 1: 38]
  76.                         else:
  77.                             df_temp = df_temp.iloc[:, 1: 16]
  78.                         print(df_temp)
  79.                         df_main = pd.concat([df_main, df_temp], axis=1)
  80.                         print(k, p, file, p2, file2)
  81.                         print(file.partition('_')[0], file2.partition('_')[0], p2)
  82.                         k += 1
  83.                         #print(file.partition('_')[0], file2.partition('_')[0], p)
  84.             #if file.partition('_')[0] == file2.partition('_')[0]:  # TODO if file != file2 ===>>>>>    upgrade__
  85.             # ___new df_main(path2[0] + file2, .....)___
  86.             # ___Save files (f'output{file}.xlsx')
  87.  
  88.             toc = time.perf_counter()
  89.             print('Time is: ', toc, f'for {file} ')
  90.             writer = pd.ExcelWriter(f"{file.partition('_')[0]}.xlsx", engine='xlsxwriter',
  91.                             datetime_format="DD-MM-YYYY")
  92.             df_main.to_excel(writer, sheet_name='Лист1', index=False, header=True)
  93.             # df_main.to_excel(f'output{file.partition(".xlsm")[0]}.xlsx', index=False, header=False, formatters={'cost':'{:,.2f}'.format})
  94.             workbook = writer.book
  95.             worksheet = writer.sheets['Лист1']
  96.             format1 = workbook.add_format({'num_format': '#,##0.00'})
  97.             worksheet.set_column(1, 168, 18, format1)
  98.             writer.save()
  99.  
  100.  
  101. for file2, path2 in file_paths_2.items():
  102.     if file2 not in check_mass:
  103.         df_main = pd.read_excel(path2[0] + file2, sheet_name='Лист1')
  104.         df_main = df_main.drop(columns=df_main.iloc[:, 1:43])
  105.         k = 1
  106.         for p2 in path2:
  107.             df_temp = pd.read_excel(p2 + file2, sheet_name='Лист1')
  108.             if p2 != './05.2020-10.2021 2.7 new/':
  109.                 df_temp = df_temp.iloc[:, 1: 38]
  110.             else:
  111.                 df_temp = df_temp.iloc[:, 1: 16]
  112.             df_main = pd.concat([df_main, df_temp], axis=1)
  113.             print(k, p2, file2)
  114.             k += 1
  115.             # print(file.partition('_')[0], file2.partition('_')[0], p)
  116.     # if file.partition('_')[0] == file2.partition('_')[0]:  # TODO if file != file2 ===>>>>>    upgrade__
  117.     # ___new df_main(path2[0] + file2, .....)___
  118.     # ___Save files (f'output{file}.xlsx')
  119.  
  120.     toc = time.perf_counter()
  121.     print('Time is: ', toc, f'for {file2} ')
  122.     writer = pd.ExcelWriter(f"{file2.partition('_')[0]}.xlsx", engine='xlsxwriter',
  123.                             datetime_format="DD-MM-YYYY")
  124.     df_main.to_excel(writer, sheet_name='Лист1', index=False, header=True)
  125.     workbook = writer.book
  126.     worksheet = writer.sheets['Лист1']
  127.     format1 = workbook.add_format({'num_format': '#,##0.00'})
  128.     worksheet.set_column(1, 168, 18, format1)
  129.     writer.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement