4
$\begingroup$

I want to prepend graphics (as generated by Plot) to an existing .nb notebook file, each new addition being placed as a new cell at the top of the notebook page. But I cannot figure out how to do it. Yes I know the function PutAppend, but this function has some problems:

  • the added graphics are not shown as graphics in the notebook, but as input code;
  • it puts the new content at the end of the file, not at the beginning;
  • the newly added part does not exist as a separate cell.

So PutAppend may not fit here. How then can it be done?

__

Edit: I hope to do it without opening the notebook file in question and to save the file automatically after adding graphics.

$\endgroup$
  • 2
    $\begingroup$ I can promise you that you don't want to do this at the string level. Load the notebook with NotebookImport. Insert Cell[BoxData@ToBoxes@yourPlot, "Input"] and the first position. Export that Notebook. $\endgroup$ – b3m2a1 Feb 13 '18 at 4:39
8
$\begingroup$

Assuming your notebook is called nb, e.g.

file = FileNameJoin[{$InstallationDirectory, "Documentation", 
    "English", "System", "ExampleData", "document.nb"}];
nb = NotebookOpen[file];

then evaluate this to stick a plot at the start:

SelectionMove[nb, Before, Notebook]
NotebookWrite[nb, Cell[BoxData@ToBoxes@Plot[x, {x, 0, 1}], "Output"]]

Edit

As per comments this can be automated so that the notebook is not visible (below I have saved to a new file so as not to overwrite the built in file used in the example):

file = FileNameJoin[{$InstallationDirectory, "Documentation", 
    "English", "System", "ExampleData", "document.nb"}];
nb = NotebookOpen[file, Visible -> False];
SelectionMove[nb, Before, Notebook];
NotebookWrite[nb, Cell[BoxData@ToBoxes@Plot[x, {x, 0, 1}], "Output"]];
NotebookSave[nb, FileNameJoin[{$TemporaryDirectory, "saved.nb"}]]
NotebookClose[nb]

If you wanted to add the same graphic to multiple files you can make a function from the above:

addGraphic[file_, graphic_Cell] := Module[{nb},

  nb = NotebookOpen[file, Visible -> False];
  SelectionMove[nb, Before, Notebook];
  NotebookWrite[nb, graphic];
  NotebookSave[nb];
  NotebookClose[nb]
  ]

addGraphic[#, 
   Cell[BoxData@ToBoxes@Plot[x, {x, 0, 1}], "Output"]] & /@ files
$\endgroup$
  • $\begingroup$ Thank you. This requires that the file be opened first; I'll adopt this method if a better method is not available. Is it possible to do it without opening that document file and to save the file automatically, as PutAppend can? That is what I had in mind originally, but I failed to express it clearly in my post. $\endgroup$ – User18 Feb 13 '18 at 4:56
  • 2
    $\begingroup$ @User18 add NotebookSave and NotebookClose and it is easily automated. You can add Visible->False if you do not want to see the notebook open. Anyway i have to go now. $\endgroup$ – Mike Honeychurch Feb 13 '18 at 5:12
  • $\begingroup$ Valuable answer; thanks a lot! $\endgroup$ – User18 Feb 13 '18 at 5:29
  • $\begingroup$ That function is great indeed. $\endgroup$ – User18 Feb 15 '18 at 2:18
3
$\begingroup$
test = Export[
   FileNameJoin[{$TemporaryDirectory, "test.nb"}],
   Notebook[{Cell[BoxData["1+1"]]}]
];

Alternatively:

Export[
  test
, Insert[
    Import[test]
  , Cell[BoxData@ToBoxes@Graphics@Disk[], "Output"]
  , {{1, 1}}
  ]
];

and check:

NotebookOpen@test
$\endgroup$
  • $\begingroup$ This is nice too. Thanks! $\endgroup$ – User18 Feb 13 '18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.