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[
FrontEnd`ExportPacket[NotebookGet@InputNotebook[], "InputText"]]
. I'd prefer something documented though, if it exists.
NotebookGet
and alike functions. Of course you'd still have to extract the relevant data from theCell
expressions. Maybe Leonid's code formating package can give pointers how to do that...? $\endgroup$