Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert xls to xlsx

Tags:

python

uno

I have some *.xls (excel 2003) files, and I want to convert those files into xlsx (excel 2007).

I use the uno python package, when I save the documents, I can set the Filter name: MS Excel 97 But there is no Filter name like 'MS Excel 2007',

How can set the the filter name to convert xls to xlsx ?

like image 776
Thomas Avatar asked Mar 29 '12 03:03

Thomas


People also ask

Can you save XLS as XLSX?

Save to Different File Formats Click the File tab. Click Save As. Click the Save as type list arrow. The default file type is an XLSX Excel file, but you can choose from plenty of other file formats in this menu.

Is XLS and XLSX same?

For compatibility, XLS has higher compatibility than XLSX. XLS is readable by all Microsoft Excel versions while XLSX is only readable by Excel 2007 and later versions. besides, XLS is able to hold the spreadsheets either including Macros or not, while XLSX isn't capable to support Macros.


2 Answers

You need to have win32com installed on your machine. Here is my code:

import win32com.client as win32 fname = "full+path+to+xls_file" excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(fname)  wb.SaveAs(fname+"x", FileFormat = 51)    #FileFormat = 51 is for .xlsx extension wb.Close()                               #FileFormat = 56 is for .xls extension excel.Application.Quit() 
like image 65
kvdogan Avatar answered Sep 19 '22 18:09

kvdogan


Here is my solution, without considering fonts, charts and images:

$ pip install pyexcel pyexcel-xls pyexcel-xlsx 

Then do this::

import pyexcel as p  p.save_book_as(file_name='your-file-in.xls',                dest_file_name='your-new-file-out.xlsx') 

If you do not need a program, you could install one additinal package pyexcel-cli::

$ pip install pyexcel-cli $ pyexcel transcode your-file-in.xls your-new-file-out.xlsx 

The transcoding procedure above uses xlrd and openpyxl.

like image 31
chfw Avatar answered Sep 20 '22 18:09

chfw