({{, (RefLink[NotebookImport, paclet:ref/NotebookImport])[notebook,style] 
imports cells with the given cell style from the specified notebook.}, {, (RefLink[NotebookImport, paclet:ref/NotebookImport])[notebook,style->form]
imports cells in the form specified by form.}})

OpenersNotesSection(Spacer1)Details and Options

In (RefLink[NotebookImport, paclet:ref/NotebookImport])[notebook,…], notebook can be a (RefLink[NotebookObject, paclet:ref/NotebookObject]), a file name, a (RefLink[CloudObject, paclet:ref/CloudObject]) corresponding to a notebook, or a (RefLink[Notebook, paclet:ref/Notebook]) expression.

If style is a pattern, cells whose style matches that pattern are imported. A typical pattern is "(Subscript[name, 1])"|"(Subscript[name, 2])"|…, indicating to import any of the styles (Subscript[name, i]).

If style is _, all cells are imported.

Possible values for form include:

The following options can be given:

With the setting "FlattenCellGroups"->(RefLink[False, paclet:ref/False]), one level of list will be present for every cell group in the notebook, whether or not any cells were imported from that group.

The setting "StyleImportRules"->(RefLink[Automatic, paclet:ref/Automatic]) is equivalent to the following mappings:

Other styles are given in "Boxes" form.

OpenersPrimaryExamplesSection(Spacer1)Examples  (13)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Create a notebook:

nb = CreateDocument[{TextCell["project title","Title"],TextCell["first section", "Section"],TextCell["details","Subsection"],ExpressionCell[Defer[2+2], "Input"],TextCell["second section","Section"],ExpressionCell[Defer[3+3],"Input"],ExpressionCell[Defer[1/0],"Input"]},WindowMargins -> {{Automatic,0},{Automatic,0}}, WindowSize -> {500,600}];
NotebookEvaluate[nb,InsertResults->True];

Import the cells in this notebook to the default forms:

NotebookImport[nb, _]

Import cells in this notebook as a list of text strings:

NotebookImport[nb, _ -> "Text"]

Import only Input style cells, as held expressions:

NotebookImport[nb, "Input"]

Import input cells as evaluatable text:

NotebookImport[nb, "Input"->"InputText"]

Import input cells as inactive expressions:

NotebookImport[nb, "Input" -> "InactiveExpression"]

Import all input and output cells from a notebook, preserving cell grouping information:

NotebookImport[nb, "Input" | "Output","FlattenCellGroups" -> False]

Import evaluatable text for every input and output cell:

NotebookImport[nb, "Input" | "Output"->"InputText","FlattenCellGroups" -> False]

OpenersExampleSection1(Spacer1)Scope  (5)

Import from a notebook file:

NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","Notebooks.nb"}],"Notes"->"Text"]

Import from a (RefLink[Notebook, paclet:ref/Notebook]) expression:

NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2","+","2"}]],"Input"]}],_->"InactiveExpression"]

The "InterpretedCell" form results in a list of (RefLink[TextCell, paclet:ref/TextCell]) and (RefLink[ExpressionCell, paclet:ref/ExpressionCell]) expressions, as appropriate:

NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2","+","2"}]],"Input"]}],_->"InterpretedCell"]//InputForm

The "HeldInterpretedCell" form returns held expressions:

NotebookImport[Notebook[{Cell[BoxData[RowBox[{"2","+","2"}]],"Input"]}],_->"HeldInterpretedCell"]//InputForm

The "Cell" form returns expressions appropriate for use in low-level notebook operations, like (RefLink[NotebookPut, paclet:ref/NotebookPut]):

NotebookPut @ Notebook @ NotebookImport[EvaluationNotebook[], "ObjectName"|"Notes" -> "Cell"];

The "Hyperlinks" form returns a list of (RefLink[Hyperlink, paclet:ref/Hyperlink]) expressions imported from each cell:

nb = FileNameJoin[{$InstallationDirectory,"Documentation","English","System","HowTos","InsertAHyperlink.nb"}];
NotebookImport[nb,"Output"->"Hyperlinks"]

OpenersExampleSection2(Spacer1)Options  (3)

OpenersExampleSubsection0(Spacer1)"FlattenCellGroups"  (2)

Import a nested textual representation of some of the cells in a notebook:

NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","Notebooks.nb"}],"ExampleText"|"Input"->"Text","FlattenCellGroups" -> False]

Import a nested textual representation of all the cells in a notebook:

NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","Notebooks.nb"}],_->"Text","FlattenCellGroups" -> False]

OpenersExampleSubsection1(Spacer1)"StyleImportRules"  (1)

Specify different forms for different cell styles:

NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","Notebooks.nb"}], "ExampleText"|"Input","StyleImportRules" -> {"ExampleText" -> "Text","Input" -> "InputText"}]

OpenersExampleSection3(Spacer1)Possible Issues  (2)

Cells that contain an incomplete expression will fail when imported to an expression-based form:

NotebookImport[Notebook[{Cell["2+","Input"]}], "Input" -> "HeldExpression"]

(RefLink[HoldComplete, paclet:ref/HoldComplete]) affects only evaluation, not typesetting:

NotebookImport[Notebook[{Cell["Grid[{{1,2},{3,4}}]","Input"]}],"Input" -> "HeldExpression"]

Use (RefLink[InputForm, paclet:ref/InputForm]) to see the untypeset form of the returned expressions:

InputForm[%]

String-based forms are inert to evaluation and most typesetting:

NotebookImport[Notebook[{Cell["Grid[{{1,2},{3,4}}]","Input"]}],"Input"->"InputText"]

OpenersExampleSection4(Spacer1)Neat Examples  (2)

Import historical information from a function's reference page:

NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","NDSolve.nb"}],"History"->"Text"]

Import the list of symbol names in the See Also links of a reference page:

strings=NotebookImport[FileNameJoin[{$InstallationDirectory,"Documentation","English","System","ReferencePages","Symbols","NDSolve.nb"}],"SeeAlso"->"Text"]

StringSplit[strings,"  ▪  "]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Import, paclet:ref/Import])  ▪  (RefLink[NotebookGet, paclet:ref/NotebookGet])  ▪  (RefLink[NotebookEvaluate, paclet:ref/NotebookEvaluate])  ▪  (RefLink[ReadList, paclet:ref/ReadList])  ▪  (RefLink[MakeExpression, paclet:ref/MakeExpression])  ▪  (RefLink[SemanticImport, paclet:ref/SemanticImport])  ▪  (RefLink[NotebookTemplate, paclet:ref/NotebookTemplate])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Cells as Wolfram Language Expressions, paclet:tutorial/CellsAsWolframLanguageExpressions])}, {▪(RefLinkPlain[Notebooks as Wolfram Language Expressions, paclet:tutorial/NotebooksAsWolframLanguageExpressions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Importing and Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Notebook Document Generation, paclet:guide/DocumentGeneration])}, {▪(RefLinkPlain[Automated Reports, paclet:guide/AutomatedReports])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Low-Level Notebook Programming, paclet:guide/LowLevelNotebookProgramming])}})}})