Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to use automator to convert excel cells to text files

I'm trying to convert each row of an excel file to a separate text file. I have two columns. The first column is the file name and the second column is the text. So, if my excel sheet is:

A1 B1

textfile1 Hello world!

textfile2 Goodbye!

How can I get automator or apple script to create two text files one named textfile1.txt (containing the text, "Hello world!") and another file named textfile2.txt (containing the text "Goodbye!")?


Thank you!!!

MacBook Pro with Retina display, OS X Yosemite (10.10.5)

Posted on Sep 24, 2015 4:16 PM

Reply
9 replies

Sep 24, 2015 9:17 PM in response to NYCBen

This is pretty easy to do. It may actually be easier in the shell, but less readable, so here's an all-AppleScript solution.


It writes the files to the desktop, but you can change that by substituting a different directory path.


tell application "Microsoft Excel"


-- define your range here:

set myRange to range "$A$1:$B$20" of worksheet 1



-- extract the first two columns of the range

set filenames to value of cells of column 1 of myRange

set file_content to value of cells of column 2 of myRange



-- iterate through them:

repeat with id from 1 to (count filenames)


-- create/open the file

set this_file to open for access (file ((path to desktop as text) & item id of filenames)) with write permission


set eofthis_fileto 0


-- write the data


writeitemid of file_contenttothis_file


-- and clean up


close accessthis_file

end repeat

end tell

If you want this as an Automator-based solution, just paste the script into a 'Run AppleScript' action.

Sep 25, 2015 4:53 AM in response to Camelot

Thanks Camelot!


This doesn't quite work. It creates one file with the correct filename (from the first cell of the worksheet), but no file contents. Then I get this syntax error: Microsoft Excel got an error: File file Macintosh HD:Users:Ben:Desktop:en_ft [this is the file name] is already open.


This is the actual script:

tell application "Microsoft Excel"

set myRange to range "$A$1:$B$45" of active sheet

set filenames to value of cells of column 1 of myRange

set file_content to value of cells of column 2 of myRange

repeat with id from 1 to (countfilenames)

set this_file to open for access (file ((path todesktopastext) & itemid of filenames)) with write permission


set eofthis_fileto 0


writeitemid of file_contenttothis_file


close accessthis_file

end repeat

end tell


Suggestions?

Sep 25, 2015 10:08 AM in response to NYCBen

Hmm, there is no error checking in my script to make sure that things are valid... are you sure there's content in the cells, for example? It would fail if the filename was empty, for example.


You could also try adding log statements to validate the data.


As for the file already being open, this can happen if the script fails mid-run - it can leave the file hanging open... the easiest way to fix it is to just throw away the file in the Finder.

Sep 25, 2015 10:51 AM in response to Camelot

All the cells have content. I modified the script to the following. It works, but for each file I have to manually choose which folder to put it in. So, I have to press choose 45 times. Not perfect, but still faster than making 45 files myself.

tell application "Microsoft Excel"

set myRange to range "$A$1:$B$45" of active sheet

set filenames to value of cells of column 1 of myRange

set file_content to value of cells of column 2 of myRange

repeat with id from 1 to (countfilenames)

set this_file to open for access (file (((choose folder) as text) & itemid of filenames)) with write permission


set eofthis_fileto 0


writeitemid of file_contenttothis_file

end repeat

end tell

Sep 25, 2015 12:34 PM in response to NYCBen

I'm not sure why the 'path to' statement would make a difference, but in any case you can simplify your woes by using 'choose folder' once outside of the loop and storing the result in a variable:


set dest_folder to (choose folder) as text


tell application "Microsoft Excel"

set myRange to range "$A$1:$B$45" of active sheet

set filenames to value of cells of column 1 of myRange

set file_content to value of cells of column 2 of myRange

repeat with id from 1 to (count filenames)

set this_file to open for access (file (dest_folder & itemid of filenames)) with write permission


set eofthis_fileto 0


writeitemid of file_contenttothis_file


close accessthis_file

end repeat

end tell


I also note that your revised script omits the 'close access this_file' line, which is needed to avoid problems.

Sep 25, 2015 12:50 PM in response to Camelot

This was closer. When I ran the script I get: Microsoft Excel got an error: Parameter error. The script editor then stops on/highlights the "close accessthis_file". If I remove the close access statement, then it works - almost. I get all the files in the specified folder, but also get a Syntax Error "Microsoft Excel got an error: An error of type -1409 has occurred." Any ideas as to why that statement could be causing an issue of some sort? Where could I put the log statements to investigate further?

Sep 25, 2015 1:09 PM in response to NYCBen

Hmm, it works when I test it here, and the nature (and location) of the errors doesn't make much sense. It means there must be something specific to your setup that's influencing this - either your version of Excel, or something in the data.


The best step (and something I probably should have done earlier) is to simplify the work we ask Excel to do (it's a bear), so try:


set dest_folder to (choose folder) as text


tell application "Microsoft Excel"

set myRange to range "$A$1:$B$45" of active sheet

set filenames to value of cells of column 1 of myRange

set file_content to value of cells of column 2 of myRange

end tell


repeat with id from 1 to (count filenames)

set this_file to open for access (file (dest_folder & itemid of filenames)) with write permission


set eofthis_fileto 0


writeitemid of file_contenttothis_file


close accessthis_file

end repeat

Ultimately, there's no reason to target Excel for anything more than the bare necessities - namely getting the cell data.

Sep 25, 2015 1:13 PM in response to Camelot

Thank you! That worked the smoothest, but I still get this Syntax Error: An error of type -1409 has occurred. The editor has this statement highlighted: open for access (file (dest_folder & itemid of filenames)) with write permission


All in all, it works and all my files are there. It's a pesky little error that doesn't seem to interfere with anything.

How to use automator to convert excel cells to text files

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