Python PyQT5-将打开窗口提示中的用户输入存储到excel文件中

Python PyQT5-将打开窗口提示中的用户输入存储到excel文件中,python,python-3.x,pyqt,pyqt5,pyqt4,Python,Python 3.x,Pyqt,Pyqt5,Pyqt4,我试图将用户的输入从打开的窗口提示符保存到excel文件中。 我在53-59之间的代码行中尝试了一些东西,但不起作用。你能帮我解决这个问题吗 此外,在每个新用户条目中,新信息应放在excel表格的底行上。它不应覆盖以前的输入 非常感谢 import sys from PyQt5.QtWidgets import QDialogButtonBox from PyQt5.QtWidgets import QFormLayout from PyQt5.QtWidgets import QLineEdi

我试图将用户的输入从打开的窗口提示符保存到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("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("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, 0, 'First Name')
sheet1.write(2, 0, 'Second Name')
sheet1.write(3, 0, 'Age')
sheet1.write(4, 0, 'Sex')
sheet1.write(5, 0, 'Marital Status')
sheet1.write(6, 0, 'Education')
sheet1.write(7, 0, '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)

xlwt库适用于旧的二进制excel文件Office 2003,仅写入文件。这就是您想要的吗?如果您运行代码,您将看到弹出一个窗口,在输入您请求的信息后,它会将信息保存到excel表中。但它现在不能正常工作,我想修复它。顺便说一句,如果有比xlwt更新的软件包,我可以用一个更新的软件包替换。请更清楚。它不能正常工作对我们来说没有任何意义。我只能假设问题是保存的文件有self.first、self.second等。不要让我们运行以查看,例如,我不能使用xlwt,请向我们解释:好的,我会尽力。问题是,;保存的excel文件不显示您输入的输入。它显示了自我。第一,自我。第二。。。。self.seven代替输入。但通常情况下,它应该显示您输入的第一个名字,第二个名字,…您输入的工作。我希望你现在明白了,谢谢。最后,这就是我想要的。非常感谢你,迈克!。