Python Forum

Full Version: reading .xls files and saving as .xlsx
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code to efficiently try to read older .xls files and save them as .xlsx. However, it keeps cutting off the first row and the last column of my data that I'm trying to copy into the new workbook. Any ideas as to how the indexing needs to be fixed to correct this problem?
-----------
import xlrd
from openpyxl.workbook import Workbook as openpyxlWorkbook
from Tkinter import *
import Tkinter, tkFileDialog
import os
from openpyxl import Workbook

workbook=Workbook()

root=Tk()

filepath=tkFileDialog.askopenfilenames()

for i in filepath:
    file, path=os.path.split(i)
    wb=xlrd.open_workbook(i)
    for k in range(0, wb.nsheets):
        xlsSheet=wb.sheet_by_index(k)
        sheet=workbook.active if k==0 else workbook.create_sheet()
        sheet.title=xlsSheet.name
        for row in xrange(1, xlsSheet.nrows):
            for col in xrange(1, xlsSheet.ncols):
                sheet.cell(row=row, column=col).value=xlsSheet.cell(row, col).value
                
        
rows and columns indexes in xlrd are zero-based and 1-based in openpyxl, so you need to adjust your code (e.g.xrange/ writing cell values)