Browse Source

Multiple lines working

Akash Singh 3 years ago
parent
commit
22e98f57d8
2 changed files with 54 additions and 51 deletions
  1. 45 42
      icopy2xls/__init__.py
  2. 9 9
      uimain.py

+ 45 - 42
icopy2xls/__init__.py

@@ -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

+ 9 - 9
uimain.py

@@ -24,6 +24,7 @@ class MainWindow(Ui_MainWindow):
         self.row_list = []
         self.filters = {}
         self.sheet_headers = []
+        self.sourceFiles = []
         self.sourceBrowsePushButton.clicked.connect(self.sourceBrowsePathSlot)
         self.destinationBrowsePushButton.clicked.connect(self.destinationBrowsePathSlot)
         self.filterButton.clicked.connect(self.filterDialog)
@@ -40,7 +41,7 @@ class MainWindow(Ui_MainWindow):
             basepath = "./"
         options = QtWidgets.QFileDialog.Options()
         options |= QtWidgets.QFileDialog.DontUseNativeDialog
-        dirName = QtWidgets.QFileDialog.getOpenFileName(
+        dirName = QtWidgets.QFileDialog.getOpenFileNames(
             None,
             "Select File ",
             basepath,
@@ -85,21 +86,20 @@ class MainWindow(Ui_MainWindow):
                 dirPath = os.path.abspath(os.curdir)
             if typ == "source":
                 self.sourcePathLineEdit.insert(dirPath)
+                self.sourceFiles = dirPath
             else:
                 self.destinationPathLineEdit.insert(dirPath)
         else:
             if typ == "source":
-                self.sourcePathLineEdit.setText(dirPath[0])
+                self.sourcePathLineEdit.setText(", ".join(dirPath[0]))
+                self.sourceFiles = dirPath[0]
             else:
                 self.destinationPathLineEdit.setText(dirPath[0])
         self.statusbar.showMessage(f"Current {typ} Path: {dirPath} ", 2000)
-        if os.path.exists(dirPath[0]):
-            if typ == "source":
-                self.getSheetsSource(dirPath[0])
-            else:
-                self.getSheetsDestination(dirPath[0])
+        if typ == "source":
+            self.getSheetsSource(dirPath[0][0])
         else:
-            self.statusbar.showMessage(f"Invalid {typ} Path: {dirPath} ", 3000)
+            self.getSheetsDestination(dirPath[0])
 
     def getSheetsSource(self, dirName):
         """ Scan for *.xlsx files and *.json files and
@@ -262,7 +262,7 @@ class MainWindow(Ui_MainWindow):
             self.statusbar.showMessage("Completed...", 3000)
 
     def check_fields(self):
-        source = self.sourcePathLineEdit.text()
+        source = self.sourceFiles
         source_sheet = self.sourceSheetComboBox.currentText()
         destination = self.destinationPathLineEdit.text()
         destination_sheet = self.destinationSheetComboBox.currentText()