|
@@ -46,6 +46,8 @@ class Mover:
|
|
|
:param lines: Number of lines starting from 1 to be considered for moving from source.
|
|
|
"""
|
|
|
self.source_file_path = source_file_path
|
|
|
+ if not isinstance(self.source_file_path, list):
|
|
|
+ self.source_file_path = [self.source_file_path]
|
|
|
self.source_sheet = source_sheet
|
|
|
self.destination_file_path = destination_file_path
|
|
|
self.destination_sheet = destination_sheet
|
|
@@ -59,48 +61,49 @@ class Mover:
|
|
|
not in destination file.
|
|
|
:return:
|
|
|
"""
|
|
|
- try:
|
|
|
- source = self.read_xlsx(self.source_file_path, self.source_sheet)
|
|
|
- destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
|
|
|
- except FileNotFoundError as e:
|
|
|
- logger.critical(f"File not found: {e}")
|
|
|
- return
|
|
|
- destination_wb = Writer(self.destination_file_path, self.destination_sheet) # Writer class object used to update existing file
|
|
|
- if add_missing_columns:
|
|
|
- self.add_missing_columns(source, destination, destination_wb)
|
|
|
- # again opening destination file as it is updated with the source file columns
|
|
|
- destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
|
|
|
-
|
|
|
- end = self.lines # if number of rows to be considered from source is not predefined the take all
|
|
|
- if not end:
|
|
|
- for i in range(source.nrows):
|
|
|
- end.append(i)
|
|
|
- new_data = [source.row(row) for row in end] # create a new list of all data and remove the filtered data
|
|
|
- remove_data = [] # rows not matching filter are stored here which is used later to remove data from new_data
|
|
|
- for filter in filters: # iterate through the dictionary of filter and
|
|
|
- ind = [x.value for x in source.row(0)].index(filter) # getting index of filter header then use the same index to check data
|
|
|
- if type(filters[filter]) is not list:
|
|
|
- filters[filter] = [filters[filter]]
|
|
|
- for row in new_data:
|
|
|
- if row[ind].value not in filters[filter]: # check if data is matching with filter
|
|
|
- remove_data.append(row)
|
|
|
- for row in remove_data: # removing unmatched data from new_data list
|
|
|
- try:
|
|
|
- new_data.remove(row)
|
|
|
- except ValueError:
|
|
|
- pass
|
|
|
- row_num = destination.nrows # used to maintain new row number
|
|
|
- for data in new_data: # iterating through the data to be written and writing then on the correct cells
|
|
|
- row_num += 1
|
|
|
- for cell in range(len(data)):
|
|
|
- try:
|
|
|
- # getting column number where new data is to be written with the help of indexing header in destination file
|
|
|
- ind = [x.value for x in destination.row(0)].index(source.row(0)[cell].value) + 1
|
|
|
- except ValueError:
|
|
|
- # if add_missing_columns is false then ValueError is thrown for the headers which are not present in destination
|
|
|
- continue
|
|
|
- destination_wb.write(row_num, ind, data[cell].value)
|
|
|
- destination_wb.save()
|
|
|
+ for source_file_path in self.source_file_path:
|
|
|
+ try:
|
|
|
+ source = self.read_xlsx(source_file_path, self.source_sheet)
|
|
|
+ destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
|
|
|
+ except FileNotFoundError as e:
|
|
|
+ logger.critical(f"File not found: {e}")
|
|
|
+ return
|
|
|
+ destination_wb = Writer(self.destination_file_path, self.destination_sheet) # Writer class object used to update existing file
|
|
|
+ if add_missing_columns:
|
|
|
+ self.add_missing_columns(source, destination, destination_wb)
|
|
|
+ # again opening destination file as it is updated with the source file columns
|
|
|
+ destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
|
|
|
+
|
|
|
+ end = self.lines # if number of rows to be considered from source is not predefined the take all
|
|
|
+ if not end:
|
|
|
+ for i in range(source.nrows):
|
|
|
+ end.append(i)
|
|
|
+ new_data = [source.row(row) for row in end] # create a new list of all data and remove the filtered data
|
|
|
+ remove_data = [] # rows not matching filter are stored here which is used later to remove data from new_data
|
|
|
+ for filter in filters: # iterate through the dictionary of filter and
|
|
|
+ ind = [x.value for x in source.row(0)].index(filter) # getting index of filter header then use the same index to check data
|
|
|
+ if type(filters[filter]) is not list:
|
|
|
+ filters[filter] = [filters[filter]]
|
|
|
+ for row in new_data:
|
|
|
+ if row[ind].value not in filters[filter]: # check if data is matching with filter
|
|
|
+ remove_data.append(row)
|
|
|
+ for row in remove_data: # removing unmatched data from new_data list
|
|
|
+ try:
|
|
|
+ new_data.remove(row)
|
|
|
+ except ValueError:
|
|
|
+ pass
|
|
|
+ row_num = destination.nrows # used to maintain new row number
|
|
|
+ for data in new_data: # iterating through the data to be written and writing then on the correct cells
|
|
|
+ row_num += 1
|
|
|
+ for cell in range(len(data)):
|
|
|
+ try:
|
|
|
+ # getting column number where new data is to be written with the help of indexing header in destination file
|
|
|
+ ind = [x.value for x in destination.row(0)].index(source.row(0)[cell].value) + 1
|
|
|
+ except ValueError:
|
|
|
+ # if add_missing_columns is false then ValueError is thrown for the headers which are not present in destination
|
|
|
+ continue
|
|
|
+ destination_wb.write(row_num, ind, data[cell].value)
|
|
|
+ destination_wb.save()
|
|
|
|
|
|
def read_xlsx(self, path, sheet):
|
|
|
# reading xlsx file using xlrd
|