3
import glob
import xlrd

from xlwt import Workbook

wb = Workbook()

for file_name in glob.glob("foo*.xls"):
    wb_orig = xlrd.open_workbook(file_name)
    for ws_orig in wb_orig.sheets():
        ws = wb.add_sheet('{0} {1}'.format(file_name, ws_orig.name))
        for rx in range(ws_orig.nrows):
            for cx in range(ws_orig.ncols):
                ws.write(rx, cx, ws_orig.cell_value(rx,cx))

wb.save("mefoo.xls")

i tried the above code in many ways to merge multiple excel sheets into one workbook........ this code gives error as

Traceback (most recent call last):
  File "E:\my python\Internship\mergestackoverflow.py", line 16, in <module>
    wb.save("mefoo.xls")
  File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 634, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 615, in get_biff_data
    self.__worksheets[self.__active_sheet].selected = True
IndexError: list index out of range

please help me to solve the error..

2 Answers 2

8

The only way that you can get that IndexError is if there are no sheets in the output workbook.

You need to examine your glob.glob("foo*.xls"); it looks like it's returning no files.

Sign up to request clarification or add additional context in comments.

1 Comment

@Cold-Blooded: Please consider "accepting" the answer (big tick to the left)
1

The solution I found was to not use an absolute path in the glob.

for file_name in glob.glob("C://your//full//path//here//foo*.xls"):
  #population/merging code here

wb.save("mefoo.xls")

As to why this solved the problem, I do not know. But I hope this helps somebody.

Comments

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.