Automator XLSX to CSV

Hi,

Is there any way to use automator to convert .XSLX files to .CSV?

Thanks.

iMac 21.5″ 4K, macOS 12.4

Posted on May 27, 2022 5:01 AM

Reply
Question marked as Top-ranking reply

Posted on May 27, 2022 9:19 AM

The script looks functional, but there are some simple syntax errors (typos, missing spaces, etc.).


Here's a cleaned up version. Try this:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--begin script

--check for an output folder and create it if necessary:
set docs_folder to (path to downloads folder)
set conversion_folder to (docs_folder as text) & "Converted Excel files"
tell application "Finder" to if not (exists folder conversion_folder) then ¬
	make new folder at docs_folder with properties {name:"Converted Excel files"}
--ask user to choose Excel files (.xlsx or xls):
set file_list to (choose file of type {"org.openxmlformats.spreadsheetml.sheet", ¬
	"com.microsoft.Excel.xls"} with multiple selections allowed)

--process each file in turn by opening in Numbers, exporting as csv to the ouput folder, then closing the original

repeat with each_file in the file_list
	
	tell application "Numbers"
		activate
		open each_file
		repeat while not (exists document 1) -- wait for Excel file to open in Numbers
		end repeat
		set file_name to name of document 1
		export document 1 to file (conversion_folder & ":" & file_name & ".csv") as CSV
		close document 1 without saving
	end tell
	
end repeat



6 replies
Question marked as Top-ranking reply

May 27, 2022 9:19 AM in response to Kathl1dd

The script looks functional, but there are some simple syntax errors (typos, missing spaces, etc.).


Here's a cleaned up version. Try this:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--begin script

--check for an output folder and create it if necessary:
set docs_folder to (path to downloads folder)
set conversion_folder to (docs_folder as text) & "Converted Excel files"
tell application "Finder" to if not (exists folder conversion_folder) then ¬
	make new folder at docs_folder with properties {name:"Converted Excel files"}
--ask user to choose Excel files (.xlsx or xls):
set file_list to (choose file of type {"org.openxmlformats.spreadsheetml.sheet", ¬
	"com.microsoft.Excel.xls"} with multiple selections allowed)

--process each file in turn by opening in Numbers, exporting as csv to the ouput folder, then closing the original

repeat with each_file in the file_list
	
	tell application "Numbers"
		activate
		open each_file
		repeat while not (exists document 1) -- wait for Excel file to open in Numbers
		end repeat
		set file_name to name of document 1
		export document 1 to file (conversion_folder & ":" & file_name & ".csv") as CSV
		close document 1 without saving
	end tell
	
end repeat



May 27, 2022 5:47 AM in response to Kathl1dd

You can try to install gnumeric via homebrew (install gnumeric) and then set an automator action with the following command:


ssconvert Excel.xls CSV.csv


Where Excel.xls is the input file and CSV.csv is the output (converted) file.


Another option is to use LibreOffice with the following command:


/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to csv Excel.xls

May 27, 2022 6:49 AM in response to Marco Klobas

So I found this code in the thread:


--begin script


--check for an output folder and create it if necessary:




set docs_folder to path to dowloads folder


set conversion_folder to (docs_folder as string) & "Converted Excel files"


tell application "Finder" to if not (exists folder conversion_folder) then ¬


  make new folder at docs_folder with properties {name:"Converted Excel files"}




--ask user to choose Excel files (.xlsx or xls):




set file_list to (choose file of type {"org.openxmlformats.spreadsheetml.sheet", ¬


  "com.microsoft.Excel.xls"} with multiple selections allowed)






--process each file in turn by opening in Numbers, exporting as csv to the ouput folder, then closing the original




  repeat with each_file in the file_list


  tell application "Numbers"


 activate




openeach_file


  repeat while not (existsdocument 1) -- wait for Excel file to open in Numbers


  end repeat


  set file_name to name of document 1


  export document 1 to file (conversion_folder & ":" & file_name & ".csv") as CSV




closedocument 1 without saving


  end tell


  end repeat


--end script


However it is giving a syntax error when I click the hammer

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator XLSX to CSV

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.