Take a look at NBDev
Basically NBDev lets you export selected cells to a python file. It is designed to construct python files and libraries and their documentation using interactive notebooks.