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()
Just wrapped the code above in a function together with better path handling to make sure the excel process is getting closed.
from pathlib import Path
import win32com.client as win32
def convert_xls_to_xlsx(path: Path) -> None:
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(path.absolute())
# FileFormat=51 is for .xlsx extension
wb.SaveAs(str(path.absolute().with_suffix(".xlsx")), FileFormat=51)
wb.Close()
excel.Application.Quit()
Answer from kvdogan on Stack Overflowpython - how to convert xls to xlsx - Stack Overflow
Convert Windows Excel File into a Mac Exc… - Apple Community
How do I convert an XLSX file into CSV? I have a 2019 M.B. Pro
Convert XLS file to XLSX without opening it?
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()
Just wrapped the code above in a function together with better path handling to make sure the excel process is getting closed.
from pathlib import Path
import win32com.client as win32
def convert_xls_to_xlsx(path: Path) -> None:
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(path.absolute())
# FileFormat=51 is for .xlsx extension
wb.SaveAs(str(path.absolute().with_suffix(".xlsx")), FileFormat=51)
wb.Close()
excel.Application.Quit()
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.
If you're comfortable with Python first install the xlrd module, which is useful for reading Excel files. You can get by doing:
% easy_install xlrd
in the Terminal.
Then give this rough script a try.
#! /usr/bin/env python
import xlrd
import csv
book = xlrd.open_workbook('an_excel_file.xls')
# Assuming the fist sheet is of interest
sheet = book.sheet_by_index(0)
# Many options here to control how quotes are handled, etc.
csvWriter = csv.writer(open('a_csv_file.csv', 'w'), delimiter='|')
for i in range(sheet.nrows):
csvWriter.writerow(sheet.row_values(i))
Run this script from the directory where the Excel file is located (an_excel_file.xls in the script) by invoking python xls_to_csv.py at the Terminal
There are a number of options for the xlrd objects, as well as the csv module, so check out the following if you need to tweak the settings:
- http://www.python-excel.org/
- Python Excel Tutorial
- CSV Module
OpenOffice/NeoOffice lets you select delimiter on CSV saves. Both are free and let you open .xls and .xlsx.
NeoOffice is the Mac port of OpenOffice, it is more Mac-like, but since it's a port of OpenOffice source it tends to trail in features just a bit. I still recommend NeoOffice of the two though.
I have dont quite a bit of Googling on this one and havent come up with a solution.
We have a large XLS file (20Mb) that will not open, Excel just kind of freezes and locks up.
I think converting it to XLSX would resolve the issue, but without opening it first I cant find a way to do this.
I have seen some online tools to do it, but this file contains sensitive info so am not happy to upload it to a random online conversion tool.
Also tried several Macro based options, but they have the same issue. When they try to open the file to convert it, Excel just locks up.
This isnt down to system resources either, very high spec machine being used has the same issue.