Python Forum
[PyQt] saving text file by FileDialog option
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] saving text file by FileDialog option
#2
add the header items to your csv writer

    def create_file_from_tableWidget(self):
        header = []
        for column in range(self.tableWidget_search.columnCount()):        
            h = self.tableWidget_search.horizontalHeaderItem(column).text()
            header.append(h)
            
        path,_ = QFileDialog.getSaveFileName(
                self, 'Save File', 'test.csv', 'CSV(*.txt)')
        if path:
            with open(path, 'w',encoding="utf-8")  as stream:
                writer = csv.writer(stream, delimiter='\t')
                writer.writerow(header)
                for row in range(self.tableWidget_search.rowCount()):
                    rowdata = []
                    for column in range(self.tableWidget_search.columnCount()):
                        item = self.tableWidget_search.item(row, column)
                        if item is not None:
                            rowdata.append(item.text())
                        else:
                            rowdata.append('')
                    writer.writerow(rowdata)
Reply


Messages In This Thread
RE: saving text file by FileDialog option - by Axel_Erfurt - Feb-08-2020, 09:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 853 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  [Tkinter] filedialog, open a file error liketocode 4 3,627 Dec-07-2022, 10:51 PM
Last Post: liketocode
  [Tkinter] logical error - saving file rwahdan 4 2,298 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  [Tkinter] Exclude hidden file, filedialog.askopenfile red380sl 1 3,248 Dec-01-2020, 07:04 PM
Last Post: DT2000
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 5,184 Dec-29-2019, 08:12 PM
Last Post: JackMack118
  Part of code is adding extra new line when saving to text file. lovepeace 9 5,283 Aug-24-2019, 12:52 PM
Last Post: lovepeace
  Update value selected in option menu when select an item from text box klllmmm 2 5,123 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  tkinter filedialog and pickle - custom icon question. kim07133 0 2,816 Jan-08-2018, 12:10 PM
Last Post: kim07133
  [Tkinter] filedialog. FULL SCREEN issac_n 0 3,203 Dec-05-2017, 07:33 AM
Last Post: issac_n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020