PyQT5- 将用户输入从打开的 window 提示存储到 excel 文件中
PyQT5- Storing users' inputs from an opening window prompt into excel file
我正在尝试将用户在打开的 window 提示中的输入保存到 excel 文件中。
我在 53-59 之间的代码行中尝试了一些东西,但它不起作用。你能帮我解决这个问题吗?
此外,在每个新用户条目中,新信息应放在 excel table 的底行。它不应覆盖以前的输入。
非常感谢
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)
我正在尝试将用户在打开的 window 提示中的输入保存到 excel 文件中。 我在 53-59 之间的代码行中尝试了一些东西,但它不起作用。你能帮我解决这个问题吗?
此外,在每个新用户条目中,新信息应放在 excel table 的底行。它不应覆盖以前的输入。
非常感谢
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)