3

I have a folder JanuaryDataSentToResourcePro that contain multiple .xlsx files. I want to iterate through folder and convert all of them into .csv and keep the same file name.

For that I'm trying to implement glob, but getting an error: TypeError: 'module' object is not callable

import glob
excel_files = glob('*xlsx*')

for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(r'''C:\Users\username\Documents\TestFolder\JanuaryDataSentToResourcePro\ResourceProDailyDataset_01_01_2018.xlsx''', 'ResourceProDailyDataset')
    df.to_csv(out) 

I am new to python. Does it look right?

UPDATE:

import pandas as pd
import glob
excel_files = glob.glob("*.xlsx")

for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'ResourceProDailyDataset')
    df.to_csv(out)

But still not converting convert .xlsx to .csv

1 Answer 1

12

The glob package should be used like:

import glob
f = glob.glob("*.xlsx")

The glob is not a method but glob.glob is.

========================================

import glob
excel_files = glob.glob('C:/Users/username/Documents/TestFolder/JanuaryDataSentToResourcePro/*.xlsx') # assume the path
for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel) # if only the first sheet is needed.
    df.to_csv(out) 
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. It doesnt give me an error anymore, but .xlsx files has not been converted to .csv for some reason.
the read_excel in your loop only read "'C:\Users\username\Documents\TestFolder\JanuaryDataSentToResourcePro\ResourceProDailyDataset_01_01_2018.xlsx". You need put excel here instead of this particular file name.
But how will it know which folder to go?
I add the solution for you and please vote if it's ok.
If only one sheet in each excel files, you can simply ignore it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.