How to batch exporting code from Python nodes?

I am currently working with Python script components in Grasshopper and have encountered a challenge that I believe others might also be facing. As part of my project documentation and possible sharing code as Jupyter Notebook, I want to export the Python scripts embedded within the Script and/or Python3 components en masse, rather than manually copying out each script one by one, and do so after each change. A manual process is time-consuming, especially when dealing with large definitions that contain multiple script components.

From my understanding and attempts (but i am no python expert), there doesn’t seem to be a straightforward method to programmatically access the Python script content directly from these components

What I’m looking for:

  • A way to programmatically extract all Python scripts from my Grasshopper document, ideally through a batch process.
  • Alternatively add a string output to each node that take it’s content. And then I append it from the panels and save it from there (hopefully ;)0

Thank you all for your help and time!
get_python_nodes_content.gh (8.4 KB)
Here is waht I have thus far, check by type and get guid

The ‘old’ GHPython components (type(comp) is GhPython.Component.ZuiPythonComponent, isinstance didn’t work) can be introspected from other components, by searching ghdoc.Component.Attributes.DocObject.OnPingDocument().Objects.
The code is simply a string in comp.Code

Found this issues … seems to work as you hinted at only for IronPythin node, not Python3. I’ll link the topic to past post, with more info
Rhino 8 Python Grasshopper Component.Code Property - Grasshopper Developer - McNeel Forum

Have you seen this? Rhino 8 Python Grasshopper Component.Code Property - #19 by eirannejad