1

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 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

Now I get this: filestr = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ) ValueError: mmap offset is greater than file size
You could get this error if size is greater than the file size of f. If that does not help, please open a new question and show how f and size are defined in more detail.
@Antoni4040. Works for me when passing a filename (even a QString) as first argument to xlrd.open_workbook. Have you checked that the file you are using is a valid xls file? Try passing the file contents in directly, like this: xlrd.open_workbook(file_contents=open(filename).read()).
@Antoni4040. If you look at the constructors for QTableWidgetItem you'll see that passing an int as the first argument will be interpreted as the item's type, rather than its contents.
Yes, but had I converted it to a string... What else can I do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.