问题描述
我试图将用户输入的内容从打开的窗口提示保存到excel文件中。 我在53-59之间的代码行中尝试了一些操作,但是它不起作用。您能帮我解决这个问题吗?
此外,在每个新用户条目中,新信息都应放在excel表的底行。它不应覆盖以前的输入。
非常感谢
import sys
from PyQt5.QtWidgets import QDialogButtonBox
from PyQt5.QtWidgets import qformlayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import *
from xlwt import Workbook
class InputDialog(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.setwindowTitle("@R_595_4045@ion Window")
self.first = QLineEdit()
self.second = QLineEdit()
self.third = QLineEdit()
self.fourth = QLineEdit()
self.fifth = QLineEdit()
self.sixth = QLineEdit()
self.seventh = QLineEdit()
dlglayout = QVBoxLayout(self)
formlayout = qformlayout()
formlayout.addRow("Fırst Name:",self.first)
formlayout.addRow("Second Name:",self.second)
formlayout.addRow("Age:",self.third )
formlayout.addRow("Sex:",self.fourth)
formlayout.addRow("Marital Status:",self.fifth)
formlayout.addRow("Education:",self.sixth)
formlayout.addRow("Job:",self.seventh)
dlglayout.addLayout(formlayout)
btns = QDialogButtonBox()
btns.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Save)
dlglayout.addWidget(btns)
btns.accepted.connect(self.accept)
btns.rejected.connect(self.reject)
def getInputs(self):
return self.first.text(),self.second.text(),self.third.text(),self.fourth.text(),self.fifth.text(),self.sixth.text(),self.seventh.text()
wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1')
sheet1.write(1,'First Name')
sheet1.write(2,'Second Name')
sheet1.write(3,'Age')
sheet1.write(4,'Sex')
sheet1.write(5,'Marital Status')
sheet1.write(6,'Education')
sheet1.write(7,'Job:')
sheet1.write(0,1,'self.first')
sheet1.write(0,2,'self.second')
sheet1.write(0,3,'self.third')
sheet1.write(0,4,'self.fourth')
sheet1.write(0,5,'self.fifth')
sheet1.write(0,6,'self.sixth')
sheet1.write(0,7,'self.seventh')
wb.save('output example.xls')
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = InputDialog()
if dialog.exec():
print(dialog.getInputs())
exit(0)
解决方法
对于较新的excel文件(xlsx),请使用openpyxl
模块。它允许读写。
这是更新的代码:
import sys
from PyQt5.QtWidgets import QDialogButtonBox
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import *
import openpyxl
filename = "data.xlsx"
def checkfile(): # create file if needed
# check if excel file exists
import os.path
from os import path
if not path.exists(filename):
# create workbook
wb = openpyxl.Workbook()
#sheet1 = wb.create_sheet('Sheet1')
ws = wb.worksheets[0]
ws.cell(2,1).value = 'First Name'
ws.cell(3,1).value = 'Second Name'
ws.cell(4,1).value = 'Age'
ws.cell(5,1).value = 'Sex'
ws.cell(6,1).value = 'Marital Status'
ws.cell(7,1).value = 'Education'
ws.cell(8,1).value = 'Job'
ws.cell(1,2).value = 'self.first'
ws.cell(1,3).value = 'self.second'
ws.cell(1,4).value = 'self.third'
ws.cell(1,5).value = 'self.fourth'
ws.cell(1,6).value = 'self.fifth'
ws.cell(1,7).value = 'self.sixth'
ws.cell(1,8).value = 'self.seventh'
wb.save(filename)
class InputDialog(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.setWindowTitle("Information Window")
self.first = QLineEdit()
self.second = QLineEdit()
self.third = QLineEdit()
self.fourth = QLineEdit()
self.fifth = QLineEdit()
self.sixth = QLineEdit()
self.seventh = QLineEdit()
dlglayout = QVBoxLayout(self)
formlayout = QFormLayout()
formlayout.addRow("First Name:",self.first)
formlayout.addRow("Second Name:",self.second)
formlayout.addRow("Age:",self.third )
formlayout.addRow("Sex:",self.fourth)
formlayout.addRow("Marital Status:",self.fifth)
formlayout.addRow("Education:",self.sixth)
formlayout.addRow("Job:",self.seventh)
dlglayout.addLayout(formlayout)
btns = QDialogButtonBox()
btns.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Save)
dlglayout.addWidget(btns)
btns.accepted.connect(self.accept)
btns.rejected.connect(self.reject)
def getInputs(self):
return self.first.text(),self.second.text(),self.third.text(),\
self.fourth.text(),self.fifth.text(),self.sixth.text(),self.seventh.text()
def writefile(data):
wb = openpyxl.load_workbook(filename)
ws = wb.worksheets[0]
# find empty column
for c in range(2,100):
if not ws.cell(2,c).value: break
# enter data
for r in range(len(data)):
ws.cell(r+2,c).value = data[r]
wb.save(filename)
if __name__ == '__main__':
checkfile() # create if needed
app = QApplication(sys.argv)
dialog = InputDialog()
if dialog.exec():
writefile(dialog.getInputs())
exit(0)