------------------ Re-running Scripts ------------------ Each time the above script, is run, it will create a new workspace and a new book, destroying any previously saved changes. It would be more useful for the script to re-use a previously created workspace and book, if they exist, instead of blindly overwritting them. If the graph frames and graphs already exist inside the book, they should be reused. This will allow static content to be added to a report workspace, and the script will be used to only update the dynamic portions. Workspace Exists? ================= Instead of blindly creating a new workspace, we will first attempt to load an existing one. If we can successfully load the workspace, we will retrieve a reference to the existing book. Only if the workspace did not exist will we create a new one. Replace the following 4 lines: .. literalinclude:: step-10.py :start-at: new_workspace :end-at: untitled.sheet with the following ``try: catch:`` structure: .. literalinclude:: step-11.py :start-at: # Workspace :end-at: book.sheet Change the last two lines of the script from: .. literalinclude:: step-10.py :start-at: save_as :end-at: save_workspace_as to the following, using the variables defined above for the folder, book and workspace names: .. literalinclude:: step-11.py :start-at: # Save the book and workspace Book Exists? ============ It is possible the workspace exists, but has been saved without the `ScriptDemo` book. In this case, the ``enerplot.book(book_name)`` command will fail. As above, we can catch that, and create the required book. Replace the ``book = enerplot.book(book_name)`` command with the following: .. literalinclude:: step-12.py :start-at: # Get reference to book :end-at: enerplot.new_book Sheet Exists? ============= Since sheets can be removed from a book, we need to ensure our required sheet is present, and create it if it isn't. Replace ``sheet1 = book.sheet('Sheet1')`` with: .. literalinclude:: step-17.py :start-at: # Locate :end-at: book.new_sheet Datafile Exists? ================ If the workspace was newly created, above, it won't have any datafile in it. If the workspace exists, it should have our desired datafile, but it is possible the datafile was removed and then the workspace was saved. So we again must look for the datafile, and load it if not present. Remove the ``enerplot.load_datafiles(...)`` line, and add the following before the code that creates the graph frame: .. literalinclude:: step-17.py :start-at: "Cigre 47" :end-at: ph_c We've shorten the variable names holding the channels here. We'll fix the references to those variables in a later step. Graph Frame? ============ Again, just because the workspace, book, and sheet all exists does not guarentee that our graph frame exists. If it does, we'll assume it still has our Phase voltage graph set up properly. If not, we'll need to create it. Replace the following lines, which create the graph frame and first graph: .. literalinclude:: step-10.py :start-at: graph_frame :end-at: graph.zoom with the following shorter code which does approximately the same thing: .. literalinclude:: step-17.py :start-at: Graph Frame :end-at: top.zoom Here, we are searching ``sheet1`` for a Graph Frame which has the given title. If it is not found, we create it, combining creating the graph frame, positioning it, sizing it and setting the title into one step. If we create the graph frame, we set the title of the first graph, add three channels to the graph in one step, and then set the zoom for the graph. Overlay Graph? ============== Finally, just because the graph frame exists doesn't mean our second overlay graph hasn't been removed. We need to check for it, and if not found, create it. Replace the two ``graph2`` lines with: .. literalinclude:: step-17.py :start-at: Zero Sequence Voltage :end-at: bottom.properties Creating New Data ================= Finally, it is time to compute and add the zero sequence voltage to the graph. Since it is possible to save the book with a zero sequence voltage channel added to the graph, but loading the book will not recreate the actual zero sequence channel, we will need to remove any previously saved channel from the graph before adding the new channel to the graph. Add the following lines: .. literalinclude:: step-17.py :start-at: # Remove :end-at: bottom.add_curves Testing the Script ================== You can run this :download:`script ` several times. It should be resilient against various changes such as deleting the workspace, and adding or deleting books, sheets, graph frames, graphs, and other components. Each time it runs, if a component exists, it should find and reuse it, or recreate it if it is missing. Any addition items you choose to save in the book and workspace should persist when the script is run.