python-3.xpandasxlrd

Unable to run pd.read_excel for scripts that previously worked


I am unable to get my python scripts working. To clarify they did work before but now they not, the code itself is not the issue. Whenever I try to run one I get an error specifically on the line pd.read_excel(file)

Traceback (most recent call last):
  File "automated_cleaning.py", line 9, in <module>
    df = pd.read_excel(roster_file)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\util\_decorators.py", line 208, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 310, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 819, in __init__
    self._reader = self._engines[engine](self._io)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_xlrd.py", line 20, in __init__
    import_optional_dependency("xlrd", extra=err_msg)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\compat\_optional.py", line 90, in import_optional_dependency
    module = importlib.import_module(name)
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 860, in get_code
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\"User"\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\__init__.py", line 1187
    print "EXTERNSHEET(b7-):"
                            ^
SyntaxError: invalid syntax

Process finished with exit code 1

After searching it seems like the file is corrupt and that is the reason why it is not working. I have tried changing the function to use a different engine but I still get a similar error.

I have tried running pip install to try and fix the package in both Pycharm Terminal and the cmd line but in either scenario it doesn't work.

Any suggestions?


Solution

  • I had similar problem in past, I just updated my xlrd module.

    python -m pip install --upgrade xlrd
    

    and also try to put r in file path

    pd.read_excel(r"C:\Users\")
    

    I hope you're using python-3.x