Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert (read & save) excel xlsx to xls

How can I convert an existing xlsx Excel file into xls while retaining my Excel file formatting? I use Anaconda Python 3, so I'm not sure I can use xlutils... I was not able to install it via conda install xlutils because of lots of incompatibilities. So now I use this code without the xlutils.copy():

import xlrd, xlwt

wb = xlrd.open_workbook(my_xlsx_excel_file)
# wb = xlutils.copy(wb)
wb.save(my_xlsx_excel_file[:-1])

And I get this error:

AttributeError: 'Book' object has no attribute 'save'

Thank you!

like image 637
ragesz Avatar asked Jun 23 '16 14:06

ragesz


People also ask

What does read () in Python?

Python File read() Method The read() method returns the specified number of bytes from the file. Default is -1 which means the whole file.

What does open () read () do in Python?

Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file.

Can you open a Python file as read write?

Also if you open Python tutorial about reading and writing files you will find that: 'r+' opens the file for both reading and writing. On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'.

How do I read text into a single string in Python?

Use file. read() to read an entire file read() to read an open text file into a string. Use str. replace() to replace all newline characters with spaces. Afterward, close the file.


1 Answers

First things first: Why do you want to convert to .xls? This is usually a sign that you are using outdated tools somewhere in the process, and it might be better to use newer tools rather than convert the data to an older format.

But, if you really need to convert to .xls while preserving formatting, your only realistic choice at this time is to use Excel itself. You didn't say which platform you are using, but if it's Windows or Mac, and you have Excel installed, then the most straightforward way to automate Excel is probably xlwings. In principle this will allow you to use Python to open the .xlsx file in Excel (an actual, running instance of Microsoft Excel) and do "save as" to a .xls file.

I say "in principle" because I don't personally know how to do it in xlwings. (I don't really use that package.) Under the covers, xlwings is relying on pywin32 on Windows and appscript on Mac, so you could use those lower-level packages directly.

For example, if you are on Windows, you could do this:

from win32com.client import Dispatch

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(my_xlsx_excel_file)
wb.SaveAs(my_xlsx_excel_file[:-1], FileFormat=56)
xl.Quit()

The 56 is a magic constant indicating Excel 97-2003 format (for Windows).

Naturally, there should be a corresponding way to do this on a Mac with appscript. Just be aware that the file format constants may be different than on Windows.

like image 63
John Y Avatar answered Oct 23 '22 02:10

John Y