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

I try to import Excel File from PyGt simple and then read it with pandas but It doesn't work.

I have error message:

Traceback (most recent call last): File "c:/Users/burea/Desktop/PROG/PYTHON/", line 32, in openFile df = pandas.read_excel(fileName) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\", line 304, in read_excel io = ExcelFile(io, engine=engine) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\", line 824, in __init__ self._reader = self._engines[engine](self._io) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\", line 21, in __init__ super().__init__(filepath_or_buffer) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\excel\", line 344, in __init__ filepath_or_buffer, _, _, _ = get_filepath_or_buffer(filepath_or_buffer) File "C:\Users\burea\Anaconda3\lib\site-packages\pandas\io\", 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):
    def initUI(self):    

        OpenF= QAction('Open', self)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')

        self.resize(800, 600)  
    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()
Do you have an idea of what is going wrong ? I had no problems doing this with Tkinter.

Thank you,
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)")
When I ran it I saw this:
('C:pythonmusings/', '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.

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)

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Python Application Not Finding Excel File dseals26 2 195 Feb-17-2021, 01:45 AM
Last Post: thewolf
  Import a file and show file name on qcombobox GMCobraz 1 435 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 3,221 Apr-17-2020, 08:14 PM
Last Post: johnjh
  PyQt5 - import rext from other file - despearte for help D_frucht 1 1,183 May-26-2018, 06:37 AM
Last Post: Barrowman
  [PyQt] cant import progress bar from another py file swipis 7 4,816 Dec-18-2016, 10:41 AM
Last Post: swipis

Forum Jump:

User Panel Messages

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