我可以将所有其他文件写入excel工作表,但是不能将图像写入excel工作表。我试图从setimage函数中获取图像文件名,这就是我遇到的问题。任何人都可以看看我的代码并给我一些指针吗?谢谢
下面是供用户输入以获取文件名的对话框窗口:
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Riwaj'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.getText()
self.show()
def getText(self):
text, okPressed = QInputDialog.getText(self, "Get text", "File Name:",
QLineEdit.Normal, "")
if okPressed and text != '':
# print(text)
file = Path(text + '.xlsx')
if file.exists():
QtWidgets.QMessageBox.about(self, "File Name", "File name
already exist")
else:
wb = openpyxl.load_workbook('hello.xlsx')
wb.save(text + '.xlsx')
else:
QtWidgets.QMessageBox.about(self, "File Name", "Enter FileName")
下面是主窗口
class AppWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(AppWindow, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.setImage)
self.pushButton_2.clicked.connect(self.addItem)
def setImage(self):
image_name, ext = QtWidgets.QFileDialog.getOpenFileName(self, 'select
photo')
if image_name:
self.lineEdit_3.setText(os.path.basename(image_name))
# self.lineEdit_3.setText(image_name)
pixmap = QtGui.QPixmap(image_name)
pixmap = pixmap.scaled(self.label_3.width(),
self.label_3.height(),QtCore.Qt.KeepAspectRatio)
self.label_3.setPixmap(pixmap)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
下面是将所有内容写入excel工作表的代码
def addItem(self):
name = self.lineEdit.text()
age = self.lineEdit_2.text()
wb = openpyxl.load_workbook('hello.xlsx')
ws1 = wb.active
ws1['A1'] = (name)
ws1['A2'] = (age)
# This is where I would like to use image name from above function
# "setImage" and write to same workbook but different sheet. I am
# having hard time getting image name from lineEdit_3 I didn't put any
# code here yet as i am not sure how i am going to do this part.
wb.save(filepath)
self.newWindow()
def newWindow(self):
self.myOtherWindow = App()
self.myOtherWindow.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
application = AppWindow()
application.show()
sys.exit(app.exec())`enter code here`