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 8347375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:20:46+00:00 2026-06-09T07:20:46+00:00

So, I have an application with a QTableWidget and want to import an .xls

  • 0

So, I have an application with a QTableWidget and want to import an .xls file:

def openfile(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '/home', ".xls(*.xls)")
        fname = open(filename)
        with fname:
            wb = xlrd.open_workbook(fname)
            wb.sheet_names()
            sh = wb.sheet_by_index(0)
            for col in sh.ncols:
                for i in col:
                    r = 0
                    c = 0
                    newItem = QtGui.QTableWidgetItem(i)
                    self.tableWidget.setItem(r, c, newItem)
                    r += 1
                r = 0
                c += 1

But, I get this error: TypeError: coercing to Unicode: need string or buffer, file found

What have I done wrong?

Update:

def openfile(self):
            filename = unicode(QtGui.QFileDialog.getOpenFileName(self, 'Open File', '', ".xls(*.xls)"))
            wb = xlrd.open_workbook(filename)
            wb.sheet_names()
            sh = wb.sheet_by_index(0)
            self.first = sh.col_values(0)
            self.r = 0
            self.add()

    def add(self):
        for i in self.first:
             str(i)
             newItem = QtGui.QTableWidgetItem(i)
             self.tableWidget.setItem(self.r, 0, newItem)
             self.r += 1

It’s working, but I can’t load numbers…
Only strings…
Weird…

Update2:

def add(self):
        for i in self.first:
             newItem = QtGui.QTableWidgetItem(str(i))
             self.tableWidget.setItem(self.r, 0, newItem)
             self.r += 1

But it displays all numbers as floats….

Update3:

def add(self):
        for i in self.first:
             try:
                newItem = QtGui.QTableWidgetItem(str(int(i)))
             except ValueError:
                newItem = QtGui.QTableWidgetItem(str(i))    
             self.tableWidget.setItem(self.r, 0, newItem)
             self.r += 1

Problem solved…

  • 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-09T07:20:47+00:00Added an answer on June 9, 2026 at 7:20 am

    xlrd.open_wookbook expects a filename (a string), not fname (a file object).

    Try:

            filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '/home', ".xls(*.xls)")
            wb = xlrd.open_workbook(filename)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application for iphone i want to open it from start up page
I have application I want a button to open another screen with done button
I have application where i can change XAML file. For example: Move and resize
I have application use SDF file and I use System.Data.SqlServerCe, Version 3.5.0.0 I make
I have application packed as an .ear file with ejb module inside. There is
I have application called unistat installed on my pc. I want to pass an
I have application with internet access and don't want to store many string.xml files
I have application that needs to load local html file if user is offline,
I have application i want to run as LaunchDaemon on OSX. My plist looks
I have application in ubuntu named school tool it uses zodb . I want

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.