Python Forum
reading .xls files and saving as .xlsx
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading .xls files and saving as .xlsx
#1
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
                
        
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .py pandas matplotlib .xlsx files QubeStory 1 787 Mar-23-2023, 09:38 AM
Last Post: buran
  comparison of two xlsx files Jlyk 4 1,951 Sep-20-2021, 07:13 AM
Last Post: Jlyk
  Reading Multiple text Files in pyhton Fatim 1 1,914 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Trouble reading files using pyexcel codebeacon 2 2,186 Feb-08-2021, 05:53 AM
Last Post: codebeacon
  Extracting information from .xlsx files hobbyist 0 1,592 Jan-06-2021, 07:20 PM
Last Post: hobbyist
  extract zip from .msg files and saving according to date stamp of email natjo 3 2,934 Aug-13-2020, 09:35 PM
Last Post: bowlofred
  Reading txt files in ssh server. therenaydin 6 4,133 Aug-06-2020, 05:17 AM
Last Post: ndc85430
  How to stop the reading of files Laplace12 4 2,354 Jul-27-2020, 04:07 PM
Last Post: Gribouillis
  Saving Files Kristenl2784 3 2,010 Jul-13-2020, 04:36 PM
Last Post: bowlofred
  Reading serial data and saving to a file Mohan 1 7,531 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020