6

I have a very long output from help() function and found it a bit hard to find what I am looking for in the script editor. Is it possible to save the help() output as a formatted html file?

vaultah
  • 44,105
  • 12
  • 114
  • 143
bwv656
  • 73
  • 6
  • there are other options outside the answers below, you can check this: [python documentation generator](http://stackoverflow.com/questions/1125970/python-documentation-generator) for more. I made a quick test with sphinx, but I find it to convoluted to my taste so I cannot get nothing out of it – Copperfield Feb 28 '16 at 01:32

3 Answers3

6

Yes, you can use the pydoc module to do this in the Python interpreter:

import pydoc
pydoc.writedoc("sys")

This code will write the documentation for the sys module to a file sys.html in the current directory. If you want to get documentation for a package, you need to use pydoc.writedocs() instead.

The Python docs say that you can also run pydoc -w <object> from the command line, but this didn't work for me without including the full path:

C:\Python27\Lib\pydoc.py -w sys
tjohnson
  • 1,047
  • 1
  • 11
  • 18
  • 1
    to run pydoc or any other module from anywhere as a script you need to do `python -m [arguments]` in this case that is `python -m pydoc -w sys` – Copperfield Feb 27 '16 at 22:12
  • Thank you so much tjohnson. I had no idea about the pydoc module. Thanks again. – bwv656 Feb 27 '16 at 22:23
1

another option that may be worth mentioning is: redirect the standard output to a file, but this will give you a text file instead of a html

for example

import sys
with open("range help.txt","w") as archi:
    t = sys.stdout
    sys.stdout = archi
    help(range)
    sys.stdout = t
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

Thank you so much for your help! Ok, I put together what I have learned from all of you so far. This prints everything in my package to a text file. Making it much easier to read and search. I know this is a bit of a hack. So please feel free to improve it. Thanks again!!

import sys, os.path, pkgutil, webbrowser
pkgpath ="C:/Users/Haggai/Documents/maya/scripts/mypackage"
my_modules = [name for _, name, _ in pkgutil.walk_packages([pkgpath])]
out_file = 'C:/Users/Haggai/Desktop/History.txt'

with open(out_file,"w") as archi:
    t = sys.stdout
    sys.stdout = archi
    for i in my_modules:
        help(i)
    sys.stdout = t

webbrowser.open('file://' + out_file)

Thank you so much for pointing that out Copperfield, This worked.

tmp_list = dir(oSel)
content = '\n'.join(lines)
with open('C:/Users/Haggai/Desktop/dir.txt',"w") as text_file:
    text_file.write(content ) 
bwv656
  • 73
  • 6
  • This works great with help() function, but it doesn't work with dir(). Anybody know how to solve this? – bwv656 Feb 27 '16 at 23:20
  • `dir` produce a list of string, it don't print anything so redirecting the stdout don't work on it, you need to process that list in the way you prefer. – Copperfield Feb 27 '16 at 23:29
  • @Copperfield Thanks I had trouble pasting code here so i just updated the answer above. Thank you so much your help! – bwv656 Feb 28 '16 at 00:03
  • to put code in the comments you need to put it in between ` `` ` , but that look better when is a one line of code like `print "hello"`, also don't use the name of build-in functions as your variable´s name, like `list` when you do that you overwrite the original – Copperfield Feb 28 '16 at 00:08
  • @Copperfield Got it. just fixed it. Learned so much today :D Thanks again! – bwv656 Feb 28 '16 at 00:11