[PyQt] Import Excel file and use pandas - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] Import Excel file and use pandas (/thread-25148.html) |
Import Excel file and use pandas - WBPYTHON - Mar-21-2020 Hello, I try to import Excel File from PyGt simple and then read it with pandas but It doesn't work. I have error message: Below is my code so far, import pandas import sys from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication, QDesktopWidget, QMainWindow, QAction, qApp, QApplication, QFileDialog class Window1(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): OpenF= QAction('Open', self) OpenF.triggered.connect(self.openFile) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('File') fileMenu.addAction(OpenF) self.resize(800, 600) self.setWindowTitle('Mapping') self.show() def openFile(self): fileName = QFileDialog.getOpenFileName(self, 'OpenFile',"", "Excel (*.xls *.xlsx)") df = pandas.read_excel(fileName) if __name__ == '__main__': app = QApplication(sys.argv) ex = Window1() sys.exit(app.exec_())Do you have an idea of what is going wrong ? I had no problems doing this with Tkinter. Thank you, RE: Import Excel file and use pandas - deanhystad - Mar-22-2020 I rewrote your program as this: import sys from PySide2.QtWidgets import QMainWindow, QApplication, QFileDialog app = QApplication(sys.argv) fileName = QFileDialog.getOpenFileName(None, 'OpenFile',"", "Python (*.py)") print(fileName)When I ran it I saw this: QFileDialog.getOpenFileName returns a tuple. Now that you know that I bet the error message you got makes sense. You'll have to extract the filename from the tuple and pass that to the panda.read_excel function.
RE: Import Excel file and use pandas - WBPYTHON - Mar-22-2020 Hello, You are right, thank you. The code below solved it. fileNameTuple = QFileDialog.getOpenFileName(self, 'OpenFile',"", "Excel (*.xls *.xlsx)") fileName = fileNameTuple[0] df = pandas.read_excel(fileName) |