How to use QfileDialog to export information from tablewidget
-
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)
-
@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. -
@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)
-
@LT-K101
Yes, sorry, for saving it should indeed beQFileDialog.getSaveFileName()
rather thanQFileDialog.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.
-
@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)
-
@LT-K101 said in How to use QfileDialog to export information from tablewidget:
Thanks I used the following code.
Does it work?