3

I have a gui I created in pyqt4 that has a function to call a module that is supposed to write into an excel sheet using pandas excelwriter. For some reason, it creates the worksheet but does not write anything into it. It just crashes my qui without any errors..when I run it in debug mode, it apparently goes through without any issues. I have been debugging this for the past few days and now point the issue between pyqt and excelwriter.Is there a known issue that pyqt does not like pandas excelwriter?

from PyQt4 import QtCore,QtGui  
import sys
from MAIN_GUI import *

if __name__=="__main__":
    app = Qt.Gui.QApplication(sys.argv)


class MAIN_GUI(QtGui.QMainWindow):
        def __init__self:
            super(MAIN_GUI, self.__init__:
            self.uiM=Ui_MainWindow
            self.uiM.setupUi(self)
            self.connect(self.uiM.updateALL_Button,QtCore.SIGNAL('clicked()'),self.updateALLEXCEL)

def updateALLEXCEL(self):
    import excel_dummy

main_gui = MAIN_GUI()
main_gui.show()
main_gui.raise_()
sys.exit(app.exec_())

---excel_dummy.py---

import pandas as pd
from pandas import ExcelWriter

def excelify():
    with ExcelWriter('/home/Desktop/Excelified/final.xlsx', engine='xlsxwriter') as writer:

        workbook=writer.book
        worksheet=workbook.add_worksheet()
        worksheet.write(2,2,'just write something')
    writer.save()
excelify()

---MAIN_GUI.py---

from PyQt4 import QtCore,QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.unicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(320,201)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.updateALL_Button = QtGui.QPushButton(self.centralwidget)
        self.updateALL_Button.setGeometry(QtCore.QRect(40,110,161,27))
        self.updateALL_Button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.updateAll_Button.setObjectName(_fromUtf8("Options_updateALL_Button"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 24))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self,MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.updateALL_Button.setText(_translate("MainWindow", "updateALL", None))
Daniel
  • 77
  • 3
  • 20
  • @ekhumoro do you think you have a solution for this one?? – Daniel Sep 29 '16 at 19:40
  • There is no way to run your example code, which is incomplete and has syntax errors. Please read the guidance on how to produce a [mcve]. – ekhumoro Sep 29 '16 at 23:23
  • I'm sorry I thought you could just look at it and see what the issue was. I edited/updated my question – Daniel Sep 30 '16 at 11:30
  • Well, I did just look at it, and I could easily see that it wouldn't produce any of the problems you describe. The same is true of the new code you posted (which is still littered with syntax errors). You need to post a proper, working test case, so that others can try to reproduce the problems you are seeing. – ekhumoro Sep 30 '16 at 15:33
  • Are you sure it's even writing the file, and that the one you see isn't just an old file? Just importing `excel_dummy` won't run the `excelify()` function. – Brendan Abel Sep 30 '16 at 21:17
  • That's the thing...it's not writing the file...it creates it..as in I see the file final.xlsx but it has 0bytes and nothing in it..obviously..what am I doing wrong? – Daniel Oct 03 '16 at 11:51
  • If I run the excelify function by itself, I'm able to produce the excel file with everything written and saved..but not with the GUI – Daniel Oct 03 '16 at 12:22
  • I forgot to add the excelify() instance in here...had it in my original problem...still same problem – Daniel Oct 03 '16 at 12:27

2 Answers2

3

The code below works for me. That is, after I click the updateALL button, it prints this:

file size: 5259 "/tmp/final.xlsx"

and viewing the resulting file shows this:

enter image description here

Note that I had to fix quite a lot of bugs to get your example to work. So make sure you use all the files below when you test it:

main.py:

import sys
from MAIN_GUI import *
from PyQt4 import QtGui, QtCore

if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)


class MAIN_GUI(QtGui.QMainWindow):
    def __init__(self):
        super(MAIN_GUI, self).__init__()
        self.uiM = Ui_MainWindow()
        self.uiM.setupUi(self)
        self.connect(self.uiM.updateALL_Button,QtCore.SIGNAL('clicked()'),self.updateALLEXCEL)

    def updateALLEXCEL(self):
        try:
            import excel_dummy
        except:
            from traceback import format_exception
            msg = ''.join(format_exception(*sys.exc_info()))
            mb = QtGui.QMessageBox()
            mb.setWindowTitle('Error')
            mb.setText('Click Show Details to get the Traceback')
            mb.setDetailedText(msg)
            mb.exec_()


main_gui = MAIN_GUI()
main_gui.show()
main_gui.raise_()
sys.exit(app.exec_())

excel_dummy.py:

import os, pandas as pd
from pandas import ExcelWriter

def excelify():
    path = '/tmp/final.xlsx'
    with ExcelWriter(path, engine='xlsxwriter') as writer:
        workbook = writer.book
        worksheet = workbook.add_worksheet()
        worksheet.write(2, 2, 'just write something')
        writer.save()
    print('file size: %s "%s"' % (os.stat(path).st_size, path))

excelify()

MAIN_GUI.py:

from PyQt4 import QtCore,QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.unicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(320,201)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.updateALL_Button = QtGui.QPushButton(self.centralwidget)
        self.updateALL_Button.setGeometry(QtCore.QRect(40,110,161,27))
        self.updateALL_Button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.updateALL_Button.setObjectName(_fromUtf8("Options_updateALL_Button"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 24))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self,MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.updateALL_Button.setText(_translate("MainWindow", "updateALL", None))
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • ok..in my example I forgot to import PyQt4 and its components...but I had it originally..the GUI is still terminating without any errors. What version of python and pandas are you using that successfully runs this program? – Daniel Oct 11 '16 at 12:48
  • I tested with python-2.7.12 and python-3.5.2, with pandas-0.19. Both give identical results. Are you testing the **exact** code in my answer? It's impossible to make any progress with this unless you do that. – ekhumoro Oct 11 '16 at 13:11
  • Yes, copied everything the same. I'm using python2.7 from anaconda2 with pandas 0.17.0. – Daniel Oct 11 '16 at 13:37
  • @Daniel. I added an error handler to `main.py`. Please try it and post the traceback (if there is one). – ekhumoro Oct 11 '16 at 13:57
  • 1
    your solution worked. Apparently the issue was that pandas needs to be 0.18.0 or higher in order for the excelwriter to function correctly with PyQt. – Daniel Oct 12 '16 at 17:08
1

Your writer.save() statement is outside the with ExcelWriter(...) as writer: block. Try running it with the statement inside the block.

with ExcelWriter('/home/Desktop/Excelified/final.xlsx', engine='xlsxwriter') as writer:
    workbook=writer.book
    worksheet=workbook.add_worksheet()
    worksheet.write(2,2,'just write something')
    writer.save()
ASGM
  • 11,051
  • 1
  • 32
  • 53