Python Forum
[PyQt] Import Excel file and use pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Import Excel file and use pandas
#1
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,
Reply
#2
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.
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Python Application Not Finding Excel File dseals26 2 2,787 Feb-17-2021, 01:45 AM
Last Post: thewolf
  Import a file and show file name on qcombobox GMCobraz 1 1,925 Jul-02-2020, 01:38 PM
Last Post: GMCobraz
  [Tkinter] Tkinter - I need to read file excel from GUI app to script file johnjh 0 10,672 Apr-17-2020, 08:14 PM
Last Post: johnjh
  PyQt5 - import rext from other file - despearte for help D_frucht 1 2,447 May-26-2018, 06:37 AM
Last Post: Barrowman
  [PyQt] cant import progress bar from another py file swipis 7 8,501 Dec-18-2016, 10:41 AM
Last Post: swipis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020