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
#11
this is not a question that i want the code ready, but i'm new to python, and i'm trying to improve my knowledge on the net.
about my code, i just changed it for a more readable appearance of the text file.

def create_file_from_tablewidget(self):
  
    header = []
    lenght=12
    for column in range(self.tableWidget.columnCount()):      
        h = self.tableWidget.horizontalHeaderItem(column).text()
        header.append(h+'\t')
                  
    path,_ = QFileDialog.getSaveFileName(
        self, 'Save File', '', '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.rowCount()):
                rowdata = []
                for column in range(self.tableWidget.columnCount()):
                    item = self.tableWidget.item(row, column)
                    if item is not None:
                        rowdata.append(item.text()+" " * (lenght - len(item.text())))
                    else:
                        rowdata.append('')
                writer.writerow(rowdata)
there is the new apparence of the text file :

[Image: i7ts.jpg]
Reply


Messages In This Thread
RE: saving text file by FileDialog option - by atlass218 - Feb-10-2020, 08:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 745 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  [Tkinter] filedialog, open a file error liketocode 4 3,522 Dec-07-2022, 10:51 PM
Last Post: liketocode
  [Tkinter] logical error - saving file rwahdan 4 2,224 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  [Tkinter] Exclude hidden file, filedialog.askopenfile red380sl 1 3,213 Dec-01-2020, 07:04 PM
Last Post: DT2000
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 5,138 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,182 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,080 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  tkinter filedialog and pickle - custom icon question. kim07133 0 2,797 Jan-08-2018, 12:10 PM
Last Post: kim07133
  [Tkinter] filedialog. FULL SCREEN issac_n 0 3,188 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