mactonight95
mactonight95

Reputation: 15

Pandas read_excel() fails on importing xlrd

I would like to convert multiple .xls files stored in my folder to .csv format. Here is what I've got so far:

import glob
import os
import csv
import pandas as pd

path = r'C:\Users\XXX\Desktop\Test'
full_path = os.path.join(path, '*.xls')

    for filename in glob.glob(full_path):

        name_xls = os.path.basename(filename)
        name_csv = name_xls.replace('.xls', '.csv')

        data_xls = pd.read_excel(name_xls)
        data_xls.to_csv(name_csv, sep=';', encoding='ASCI')

Even that I downloaded pandas and xlrd libraries, I get the following error:

Traceback (most recent call last):   File
"C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 261, in __init__
  **import xlrd ModuleNotFoundError: No module named 'xlrd'**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File
"C:\Users\XXX\Desktop\coverage_code_0\coverage_code_0.py", line
16, in <module>
  data_xls = pd.read_excel(name_xls)   File  
C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\util\_decorators.py",
line 118, in wrapper
  return func(*args, **kwargs)   File "C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 230, in read_excel
  io = ExcelFile(io, engine=engine)   File "C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 263, in __init__
  raise ImportError(err_msg) ImportError: Install xlrd >= 0.9.0 for Excel support

import xlrd doesn't work, when I include that compiler says:

No module named 'xlrd'

I believe that there is a mistake in my code but I don't know where. Any thoughts?

Upvotes: 1

Views: 5050

Answers (2)

Oumab10
Oumab10

Reputation: 716

If you do this, you want need this package:

def convert_to_csv():
    PATH = path_to_excel
    fileNames = os.listdir(PATH)
    fileNames = [file for file in fileNames if '.xls' in file]
    for file in fileNames: 
        exl =  pd.read_excel(PATH+file)
        exl.to_csv(PATH+file[:-3]+'csv',sep=';', index=False, header=True)
if __name__ == "__main__":
    import pandas as pd
    convert_to_csv()

Upvotes: 0

u354356007
u354356007

Reputation: 3215

You need to run pip install xlrd within the same interpreter and virutalenv you have pandas in. In your comment you say that you have xlrd installed in c:\users\XXX\appdata\local\programs\python\python36-32, but your pandas is in C:\Users\XXX\.thonny\BundledPython36\. If you don't use a virtualenv, try finding pip inside BundledPython36 folder and run it.

C:\Users\XXX\.thonny\BundledPython36\...\pip install xlrd

If you use a virtualenv, activate it and simply run pip install xlrd.

Upvotes: 3

Related Questions