# Converting a notebook to plain text programmatically

It is possible to export a notebook to plain text format (practically just extracting all the text from it and discarding formatting) using the File -> Save As... menu item.

How can a notebook be exported to plain text programmatically, without making use of any GUI?

With all the other document formats, such as PDF, HTML, NB, LaTeX, etc. it is possible to simply use Export:

ExportString[NotebookGet@EvaluationNotebook[], "LaTeX"]


This does not work with the export format "Text" though. It simply exports the notebook expression in a Mathematica expression format. Is there an export format which will export the contents of the notebook to plain text instead of the expression representing it?

I would prefer a method that can produce an in-memory string as well (just like ExportString), and can avoid writing on disk.

EDIT: Based on this answer, I found First@FrontEndExecute[ FrontEndExportPacket[NotebookGet@InputNotebook[], "InputText"]]. I'd prefer something documented though, if it exists.

-
You probably know that you can get the notebook contents with NotebookGet and alike functions. Of course you'd still have to extract the relevant data from the Cell expressions. Maybe Leonid's code formating package can give pointers how to do that...? –  sebhofer May 13 '12 at 12:45

Saving is done by the front end, which you can exploit programatically by using FrontEndExecute. I think this is what you need:

nb=InputNotebook[];
fn=FileNameJoin[{"output.txt"}];
FrontEndExecute[FrontEndToken[nb,"Save",{fn,"Text"}]]


Edit: Of course you can also save it as a package by replacing "Text" with "Package".

-
Thanks! I didn't realize it was possible to avoid a save dialog popping up. I'd still like to have a solution that can export to a string though. I updated my question with this. Many Export operations are done by the front end under the hood, and are implemented in terms of ExportPacket. I guess it would be possible to use ExportPacket (I haven't yet tried), but I'd prefer something documented. –  Szabolcs May 13 '12 at 12:28

If you want to completely bypass the FE you can use:

Import["your.nb", "Plaintext"]
`

This will return a textual representation of the notebook file in the form of a String expression, which you can then post-process or export however you see fit.

The results are not perfect, but they are decent. This is the mechanism used by the Spotlight plugin on Mac, the Google Desktop and other plugins on Windows, and the comparable mechanism on Linux (I forget the name).

-
Does this really bypass the FE, i.e. it doesn't call it even in the background as it happens when exporting graphics? –  Szabolcs May 21 '12 at 15:00
It does bypass the FE. To verify: \$ /Applications/Mathematica.app/Contents/MacOS/MathKernel Mathematica 8.0 for Mac OS X x86 (64-bit) Copyright 1988-2010 Wolfram Research, Inc. In[1]:= Links[] Out[1]= {} In[2]:= Import["2.nb", "Plaintext"]; In[3]:= Links[] Out[3]= {LinkObject[/Applications/Mathematica.app/SystemFiles/Converters/Bina\ > ries/MacOSX-x86-64/NBImport.exe, 1, 1]} In[4]:= –  ragfield May 22 '12 at 1:40