0

I have some data on a TableWidget that will be exported to an excel table after clicking a button , and it works fine.

Now, I want to make it possible to choose the directory where to save the exported excel file because to_excel is saving it automatically at a predefined path.

I tried to use the QFileDialog.getSaveName() but I think I did it wrong!

Any help with this, I am a beginner in programming.

I really appreciate any help you can provide.

I am getting this Error :

raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'") OSError: Cannot save file into a non-existent directory: '('C:\Downloads'

Here is the code :

class Calculation_window(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("C:\\Users\\MAkh\\MainProgram_for_Dev\\Main_program_PyQt5\\caculated_results_window.ui",self)
        self.ExportButton1.clicked.connect(self.Export_to_Excel)
        
    def Export_to_Excel(self):

        # create column header and data frame:
        columnHeaders=[]
        for j in range(self.results_table_1.model().columnCount()):
            columnHeaders.append(self.results_table_1.horizontalHeaderItem(j).text())
            
        self.df=pd.DataFrame(columns=columnHeaders)

        # inserting data in the Data Frame:

        for row1 in range(self.results_table_1.rowCount()):
            for col in range(self.results_table_1.columnCount()):
                self.df.at[row1,columnHeaders[col]]= self.results_table_1.item(row1,col).text()

        for row2 in range(self.results_table_2.rowCount()):
            for col2 in range(self.results_table_2.columnCount()):
                row2s=row2+self.results_table_2.rowCount()
                self.df.at[row2s,columnHeaders[col2]]= self.results_table_2.item(row2,col2).text()
        

# the problem is here :

        response=QFileDialog.getSaveFileName(self,'Save the file')

        self.df.to_excel(rf'{response}.xlsx',index=False)

I tried to look for any answers on the internet, but unfortunately, I couldn't find something helpful.

MoMo
  • 23
  • 5

0 Answers0