Microsoft Excel - Save CSV as XLSX

Hi,

I have a CSV file open in Excel and I want to write some Applescript to save it in .xlsx file format. I have tried to save in the normal way, but Excel saves the CSV as a text file.

Can anyone help?

This is what I have so far:

set workbookName to "Testing"
set destinationPath to (path to desktop as text) & workbookName

tell application "Microsoft Excel"
	set name of active sheet to "Sheet1"
	tell workbook 1
		tell sheet 1
			save active workbook in destinationPath as Excel Workbook file format
		end tell
	end tell
end tell

Due to copyrights I cannot give you my CSV/XLS/XLSX osax but before that I’ve been working with Python. Python does have xlsx modules where it can easily read CSV data and write it away as an xlsx file. I used openpyxl module but there is also an xlsxwriter module for Python as well. After you’ve installed the modules openpyxl you can use the code below:

set theFile to "Macintosh HD:Users:itsme:Desktop:test.csv"

set pythonScript to "import os
import glob
import csv
import openpyxl # from https://pythonhosted.org/openpyxl/ or PyPI (e.g. via pip)

wb = openpyxl.Workbook()
ws = wb.active
csvfile = '" & POSIX path of theFile & "'
with open(csvfile, 'rU') as f:
	reader = csv.reader(f, delimiter=';')
	for r, row in enumerate(reader, start=1):
		for c, val in enumerate(row, start=1):
			ws.cell(row=r, column=c).value = val
wb.save(csvfile + '.xlsx')"

do shell script "echo " & quoted form of pythonScript & " | python"

Your script works with a minor adjustment to the output format.


set workbookName to "Testing"
set destinationPath to (path to desktop as text) & workbookName

tell application "Microsoft Excel"
	set name of active sheet to "Sheet1"
	tell workbook 1
		tell sheet 1
			save active workbook in destinationPath as Excel XML file format
		end tell
	end tell
end tell

Thanks for this solution SurferDude :slight_smile:
That looks like it’s working.

The Python script Im trying to run on the .xlsx file it creates doesn’t not recognise the file format. Any ideas? If I save the same file manually, it seems to work ok.

When I inspect the files side-by side, the xlsx created by the script is a ‘Microsoft Excel workbook’ but the manually saved (working) file is a ‘Microsoft Excel Document’

Is there a way to save the file as a document, rather than as a workbook?

Try adding the .xslx suffix to the file name.

Thats done it!
Thanks!!!

I’m trying something similar, just can’t get it to work at all…


set INPUT_FILE_NAME to “/Users/ray/Dropbox/Public/DMX-LEADS.csv”

set OUTPUT_FILE_NAME to “/Users/ray/Dropbox/Public/converted.csv”

tell application “Microsoft Excel”
activate

open INPUT_FILE_NAME

tell active workbook to save workbook as filename OUTPUT_FILE_NAME file format CSV Windows file format

close active workbook

end tell


Any ideas and Thanks in advance.

Are you sure that Excel accept POSIX pathnames ?

Yvan KOENIG running High Sierra 10.13.4 in French (VALLAURIS, France) mercredi 9 mai 2018 16:07:53

Sorry I don’t know.

I do know that this opens the file OK and closes it, but does not save the output file.

Just trying to re-save file as Windows CSV as I am on Mac and eBay File Exchange does not accept Mac CSV.

Hi.

In haolesurferdude’s script above, and in scripts for many other applications, the save destination is the ‘save’ command’s ‘in’ parameter. The ‘as’ parameter is for the file format.

I don’t have Excel, but I guess your script would need to look something like this:

set DropboxPublicPath to (path to home folder as text) & "Dropbox:Public:"

set INPUT_FILE_NAME to DropboxPublicPath & "DMX-LEADS.csv"

set OUTPUT_FILE_NAME to DropboxPublicPath & "converted.csv"

tell application "Microsoft Excel"
	activate
	
	open INPUT_FILE_NAME
	
	save active workbook in OUTPUT_FILE_NAME as CSV Windows file format
    
	close active workbook
    
end tell

You may need to put the word ‘file’ between ‘open’ and ‘INPUT_FILENAME’ and between ‘in’ and ‘OUTPUT_FILE_NAME’. I’m assuming that ‘CSV Windows’ is the correct term for the format.

Thanks Nigel, That seems to have cracked it.

Just need to put in a command to save without the dialogue box and it will be perfect.

I use scripts a bit but very simple ones, must try to study up a bit.

Cheers