November 17, 2004
Curious George: Excel to Text?
I need to take a column of excel data and somehow export it into a series of text files...
I have a spreadsheet, within which one of the columns contains words and such. I need to take those words and somehow copy them into a series of .txt files, one text file holding a cell's worth of text from the column. Talking about 1500 rows, so I really don't want to copy/paste if I don't have to. Anyone know a nice quick way to do this? I've looked over exporting as a text file of some kind, but the best I can do is get the entire columsn worth of text into a single .txt, which isn't what I want. Any thoughts or comments are welcomed. If it matter, I know eactly bupkis about scripting and macros.
<code>
works here.Filepath = InputBox("Enter a filename, including full path (row number will be appended)", "", "C:\xceldata") For i = 1 To Selection.Rows.Count Filename = Filepath & i & ".txt" Open Filename For Output As #1 ' Open file for output. Print #1, Selection.Cells(i, 1).Value ' Write data. Close #1 ' Close file. Next i
Or download it on an excel sheet from here. Now it's back to work.\n
-- a carriage return, and you have Python (the .exe, not win32all) installed..)# open the .txt file containing the column input = open(r'c:\path\to\colums.txt','r') i = 0 # write the content of each cell N in c:\path\to\cellN.txt for cell in input: fname = r'c:\path\to\cell' + i + '.txt' output = open(fname,'w') output.write(cell) output.close() i += 1 input.close()
In Python, the indenting is mandatory and significant. -- this code does not check for exceptions. Lines starting with # indicate comments.