Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to use QfileDialog to export information from tablewidget
Forum Updated to NodeBB v4.3 + New Features

How to use QfileDialog to export information from tablewidget

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 946 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    Hi,
    I want the user to choose the location to export the tablewidget data. The code below export data to the download folder without asking the user the preferred location. I need assistance on how to achieve this please. Thanks in advance.

     def export_userReport_table(self, printer):
    
         
         columnHeaders = []
    
         # create column header list
         for j in range(self.ui.tableWidget_10.model().columnCount()):
                columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
          df = pd.DataFrame(columns=columnHeaders)
    
         ####### create dataframe object recordset #########
          for row in range(self.ui.tableWidget_10.rowCount()):
                  for col in range(self.ui.tableWidget_10.columnCount()):
                          df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
    
          df.to_excel('report.xlsx', index=False)
          df.print_(printer)         
    
    JonBJ 1 Reply Last reply
    0
    • L LT-K101

      Hi,
      I want the user to choose the location to export the tablewidget data. The code below export data to the download folder without asking the user the preferred location. I need assistance on how to achieve this please. Thanks in advance.

       def export_userReport_table(self, printer):
      
           
           columnHeaders = []
      
           # create column header list
           for j in range(self.ui.tableWidget_10.model().columnCount()):
                  columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
            df = pd.DataFrame(columns=columnHeaders)
      
           ####### create dataframe object recordset #########
            for row in range(self.ui.tableWidget_10.rowCount()):
                    for col in range(self.ui.tableWidget_10.columnCount()):
                            df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
      
            df.to_excel('report.xlsx', index=False)
            df.print_(printer)         
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @LT-K101
      Just use e.g. static PySide2.QtWidgets.QFileDialog.getOpenFileName([parent=None[, caption=""[, dir=""[, filter=""[, selectedFilter=""[, options=QFileDialog.Options()]]]]]]) to get the user to pick a directory and choose/type a filename. QFileDialog is just to ask the user for the path, you write your code to actually save the data there quite separately.

      L 1 Reply Last reply
      1
      • JonBJ JonB

        @LT-K101
        Just use e.g. static PySide2.QtWidgets.QFileDialog.getOpenFileName([parent=None[, caption=""[, dir=""[, filter=""[, selectedFilter=""[, options=QFileDialog.Options()]]]]]]) to get the user to pick a directory and choose/type a filename. QFileDialog is just to ask the user for the path, you write your code to actually save the data there quite separately.

        L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #3

        @JonB I tried using the QFileDialog.getSaveFileName() because I want the user to choose location to save the TableWidget data. The code below does not work when the dialog opens and I click on the save button.

         def export_userReport_table(self, printer):
        
                     
            self.filename, ok = QFileDialog.getSaveFileName(self, "Select Location To Export File", "Exported Report(*.xlsx)")
            if ok:
                  columnHeaders = []
        
                # create column header list
                  for j in range(self.ui.tableWidget_10.model().columnCount()):
                        columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
        
                   df = pd.DataFrame(columns=columnHeaders)
        
           ####### create dataframe object recordset #########
                   for row in range(self.ui.tableWidget_10.rowCount()):
                           for col in range(self.ui.tableWidget_10.columnCount()):
                                   df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
        
                    #df.to_excel('report.xlsx', index=False)
                    #df.print_(printer)
        
        jsulmJ JonBJ 2 Replies Last reply
        0
        • L LT-K101

          @JonB I tried using the QFileDialog.getSaveFileName() because I want the user to choose location to save the TableWidget data. The code below does not work when the dialog opens and I click on the save button.

           def export_userReport_table(self, printer):
          
                       
              self.filename, ok = QFileDialog.getSaveFileName(self, "Select Location To Export File", "Exported Report(*.xlsx)")
              if ok:
                    columnHeaders = []
          
                  # create column header list
                    for j in range(self.ui.tableWidget_10.model().columnCount()):
                          columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
          
                     df = pd.DataFrame(columns=columnHeaders)
          
             ####### create dataframe object recordset #########
                     for row in range(self.ui.tableWidget_10.rowCount()):
                             for col in range(self.ui.tableWidget_10.columnCount()):
                                     df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
          
                      #df.to_excel('report.xlsx', index=False)
                      #df.print_(printer)
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LT-K101 Where in this code do you write anything to the file user selected?!

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          2
          • L LT-K101

            @JonB I tried using the QFileDialog.getSaveFileName() because I want the user to choose location to save the TableWidget data. The code below does not work when the dialog opens and I click on the save button.

             def export_userReport_table(self, printer):
            
                         
                self.filename, ok = QFileDialog.getSaveFileName(self, "Select Location To Export File", "Exported Report(*.xlsx)")
                if ok:
                      columnHeaders = []
            
                    # create column header list
                      for j in range(self.ui.tableWidget_10.model().columnCount()):
                            columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
            
                       df = pd.DataFrame(columns=columnHeaders)
            
               ####### create dataframe object recordset #########
                       for row in range(self.ui.tableWidget_10.rowCount()):
                               for col in range(self.ui.tableWidget_10.columnCount()):
                                       df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
            
                        #df.to_excel('report.xlsx', index=False)
                        #df.print_(printer)
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @LT-K101
            Yes, sorry, for saving it should indeed be QFileDialog.getSaveFileName() rather than QFileDialog.getOpenFileName().

            Please think/read about what it does. As @jsulm says, and I wrote earlier:

            QFileDialog is just to ask the user for the path, you write your code to actually save the data there quite separately.

            All it does is return the path to a file entered by the user. That's it. It does not open that file, or write anything to it. So... you need to use that to get the desired file path. Then you have to write code to open that file for write, write whatever you want saved there, and close it.

            1 Reply Last reply
            2
            • jsulmJ jsulm

              @LT-K101 Where in this code do you write anything to the file user selected?!

              L Offline
              L Offline
              LT-K101
              wrote on last edited by LT-K101
              #6

              @jsulm and @JonB Thanks I used the following code.

              filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '','Excel files (*.xlsx)')
              if ok:
                  
                    columnHeaders = []
              
                    # create column header list
                    for j in range(self.ui.tableWidget_10.model().columnCount()):
                    columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
              
                    df = pd.DataFrame(columns=columnHeaders)
              
                    ####### create dataframe object recordset #########
                    for row in range(self.ui.tableWidget_10.rowCount()):
                        for col in range(self.ui.tableWidget_10.columnCount()):
                                 df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
                    df.to_excel(filename, index=False)
              
              jsulmJ 1 Reply Last reply
              0
              • L LT-K101

                @jsulm and @JonB Thanks I used the following code.

                filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '','Excel files (*.xlsx)')
                if ok:
                    
                      columnHeaders = []
                
                      # create column header list
                      for j in range(self.ui.tableWidget_10.model().columnCount()):
                      columnHeaders.append(self.ui.tableWidget_10.horizontalHeaderItem(j).text())
                
                      df = pd.DataFrame(columns=columnHeaders)
                
                      ####### create dataframe object recordset #########
                      for row in range(self.ui.tableWidget_10.rowCount()):
                          for col in range(self.ui.tableWidget_10.columnCount()):
                                   df.at[row, columnHeaders[col]] = self.ui.tableWidget_10.item(row, col).text()
                      df.to_excel(filename, index=False)
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @LT-K101 said in How to use QfileDialog to export information from tablewidget:

                Thanks I used the following code.

                Does it work?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved