How to extract sheet from multiple *.xlsm and save it as *.csv or txt in Python?

My requirement is exactly matching with below post from "StackOverflow". This code snippet works perfectly for only one excel file (workbook = xlrd.open_workbook('test.xlsx')) but I have more than 100 + excel files with same Sheet name across all the excel macro files.

Can someone please modify the code, which can create multiple files with different name



import csv
import xlrd

workbook = xlrd.open_workbook('test.xlsm')
for sheet in workbook.sheets():
    if sheet.name == "Sheet_name_from_xlsm_file":
        with open('{}.csv'.format(sheet.name), 'w') as f:
            writer = csv.writer(f)
            for row in range(sheet.nrows):
                out = []
                for cell in sheet.row_values(row):
                    try:
                        out.append(cell.encode('utf8'))
                    except:
                        out.append(cell)
                writer.writerow(out)

Open in new window

IdleGuysAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Do you want to save the data from the specified sheet in each xlsm file to it's own CSV/text file or should all the data go in the one file?

Where will all the source files be located?
0
 
IdleGuysAuthor Commented:
Hi Norie, Apologies for delay response. I was looking to extract the data to different files (csv or txt or sql) files with excel name as file name. I have completed this today with help from my team. It is working perfectly.

Thanks for your time, closing this post.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.