Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:53:44+00:00 2026-06-14T20:53:44+00:00

So, I have a QTableWidget that I want to save it to an .xls

  • 0

So, I have a QTableWidget that I want to save it to an .xls file using the xlwt module…

Here’s the code:

def savefile(self):
        filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
        wbk = xlwt.Workbook()
        self.sheet = wbk.add_sheet("sheet")
        self.row = 0
        self.col = 0
        self.add2(self.row, self.col)
        wbk.save(filename)    


def add2(self, row, col):
    for i in range(self.tableWidget.columnCount()):
        for x in range(self.tableWidget.rowCount()):
            try:
                teext = str(self.tableWidget.item(row, col).text())
                self.sheet.write(row, col, teext)
                row += 1
            except AttributeError:
                pass                   
        col += 1

But that writes out only the text from cell 0,0 and nothing else…

I think that I have made some serious mistake…

Update:

def savefile(self):
        filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
        wbk = xlwt.Workbook()
        self.sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
        self.add2()
        wbk.save(filename)    


def add2(self):
    row = 0
    col = 0         
    for i in range(self.tableWidget.columnCount()):
        for x in range(self.tableWidget.rowCount()):
            try:             
                teext = str(self.tableWidget.item(row, col).text())
                self.sheet.write(row, col, teext)
                row += 1
            except AttributeError:
                row += 1
        row = 0
        col += 1             

Solved the problem…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:53:46+00:00Added an answer on June 14, 2026 at 8:53 pm

    You might also find it more concise and easier to use the output of the range (or xrange) as the indexes for your tableWidget.item call rather than worrying about incrementing your own counters. You might be using the sheet itself in other places in code, but if you’re not, it would save you some memory to not assign the sheet to be an attribute variable of your class:

    def savefile(self):
        filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
        wbk = xlwt.Workbook()
        sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
        self.add2(sheet)
        wbk.save(filename)
    
    def add2(self, sheet):
        for currentColumn in range(self.tableWidget.columnCount()):
            for currentRow in range(self.tableWidget.rowCount()):
                try:
                    teext = str(self.tableWidget.item(currentRow, currentColumn).text()
                    sheet.write(currentRow, currentColumn, teext)
                except AttributeError:
                    pass
    

    Because you are using the range command, the currentColumn variable will increment from 0 to columnCount() and currentRow will increment from 0 to currentRow()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
I have some code creating a QTabWidget from Python using PyQt4. I want to
I have a subclass of QTableWidget with the following code: connect(this, SIGNAL(cellChanged(int, int)), this,
I have a QTableWidget and would like that pressing Ctrl while clicking on a
I have a QTableWidget. In its cells I need to display 3-state QSliders, that
I have created a table using qtablewidget. The table contains a few buttons. I
I have what is hopefully a simple problem using a QTableWidget in Qt Designer
I have a widget MyTable that contains a QTableWidget *table . This QTableWidget has
I have a QTableWidget that has a combination of QLineEdit and normal table cell
I have a QTableWidget in my program that I use to display results and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.