Python Forum
[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:

Error:
Traceback (most recent call last): File "c:/Users/burea/Desktop/PROG/PYTHON/EXFORUM.py", line 32, in openFile df = pandas.read_excel(fileName) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel io = ExcelFile(io, engine=engine) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 824, in __init__ self._reader = self._engines[engine](self._io) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py", line 21, in __init__ super().__init__(filepath_or_buffer) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 344, in __init__ filepath_or_buffer, _, _, _ = get_filepath_or_buffer(filepath_or_buffer) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\common.py", line 200, in get_filepath_or_buffer raise ValueError(msg) ValueError: Invalid file path or buffer object type: <class 'tuple'>
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:
Output:
('C:pythonmusings/sandbox.py', 'Python (*.py)')
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)