PYQT4无法写入excelwriter
PYQT4 not able to write into excelwriter
我有一个在 pyqt4 中创建的图形用户界面,它具有调用模块的功能,该模块应该使用 pandas excelwriter 写入 excel sheet .出于某种原因,它创建了 worksheet 但没有向其中写入任何内容。它只是让我的 qui 崩溃而没有任何错误。当我 运行 它处于调试模式时,它显然没有任何问题地通过。过去几天我一直在调试这个,现在指出 pyqt 和 excelwriter.Is 之间的问题有一个已知问题,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))
您的 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()
下面的代码适合我。也就是说,在我点击 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))
我有一个在 pyqt4 中创建的图形用户界面,它具有调用模块的功能,该模块应该使用 pandas excelwriter 写入 excel sheet .出于某种原因,它创建了 worksheet 但没有向其中写入任何内容。它只是让我的 qui 崩溃而没有任何错误。当我 运行 它处于调试模式时,它显然没有任何问题地通过。过去几天我一直在调试这个,现在指出 pyqt 和 excelwriter.Is 之间的问题有一个已知问题,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))
您的 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()
下面的代码适合我。也就是说,在我点击 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))