pythonexcelxlrdxlwtxlutils

Copying only worksheet of XLS to become new worksheet in new XLS using Python?


Using the Python test code below I am trying to copy the only worksheet in an Excel (*.xls) file into a new Excel file with one worksheet.

The input spreadsheet looks like:

enter image description here

from copy import deepcopy
from xlrd import open_workbook
from xlutils.copy import copy as copy
from xlwt import Workbook

rb = open_workbook(r"C:\Temp\test1.xls",formatting_info=True)
wb = copy(rb)
new_book = Workbook()
r_sheet = rb.sheet_by_index(0)

sheets = []
w_sheet = deepcopy(wb.get_sheet(0))
w_sheet.set_name("test1")

for row_index in range(0, r_sheet.nrows):
    for col_index in range(0, r_sheet.ncols):
        cell_value = r_sheet.cell(row_index, col_index).value
        print cell_value
        w_sheet.write(row_index, col_index, cell_value)

sheets.append(w_sheet)
new_book._Workbook__worksheets = sheets

new_book.save(r"C:\Temp\test2.xls")

If I run the code it shows the output below and creates the new Excel file with the worksheet named test1.

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Col1
Col2
Col3
a
1.0
X
b
2.0
Y
c
3.0
Z
>>> 

Unfortunately, the output, which appears to have the correct number of cells written to it has all non-numeric cell values written as #VALUE!.

enter image description here

Using Python 2.7.10, is there a simple way to read the worksheet from one XLS and then write it as a worksheet in another XLS file?

I do not want to simply copy the spreadsheet and then rename the worksheet in the new one because once I can get this to work I want to copy the only worksheet in each of a dozen spreadsheets to become a worksheet of the same name in a spreadsheer with a dozen worksheets.


Solution

  • from xlrd import open_workbook
    from xlutils.copy import copy
    
    # Read the workbook
    rb = open_workbook("input.xls", formatting_info=True)
    
    # Copy into a new workbook
    wb = copy(rb)
    
    # Rename to 'test1' as the original post asked for 
    ws = wb.get_sheet(0)
    ws.name = 'test1'
    
    # Save the new workbook
    wb.save("output.xls")