我有一个我在pyqt4中创建的gui,它具有调用应该使用pandas excelwriter写入excel表的模块的功能。出于某种原因,它会创建工作表但不会在其中写入任何内容。它只是崩溃我的qui没有任何错误..当我在调试模式下运行它,它显然没有任何问题。我过去几天一直在调试这个问题,现在指出pyqt和excelwriter之间的问题。有一个已知的问题,pyqt不喜欢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))
答案 0 :(得分:3)
以下代码适合我。也就是说,在我点击updateALL
按钮后,它会打印出来:
file size: 5259 "/tmp/final.xlsx"
并查看生成的文件显示:
请注意,我必须解决很多错误才能使您的示例正常工作。因此,请确保在测试时使用所有以下文件:
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))
答案 1 :(得分:1)
您的writer.save()
语句位于with ExcelWriter(...) as writer:
块之外。尝试使用块内的语句运行它。
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()