Python > 将 help() 输出保存到格式化的 html 文件?

Python > Save help() output to formated html file?

help() 函数的输出很长,我发现很难在脚本编辑器中找到我要找的东西。是否可以将 help() 输出保存为格式化的 html 文件?

是的,您可以在 Python 解释器中使用 pydoc 模块来执行此操作:

import pydoc
pydoc.writedoc("sys")

此代码会将 sys 模块的文档写入当前目录中的文件 sys.html。如果你想获得一个包的文档,你需要使用 pydoc.writedocs() 代替。

Python docs 说您也可以从命令行 运行 pydoc -w <object>,但是如果不包括完整路径,这对我不起作用:

C:\Python27\Lib\pydoc.py -w sys

另一个可能值得一提的选项是:将标准输出重定向到一个文件,但这会给你一个文本文件而不是 html

例如

import sys
with open("range help.txt","w") as archi:
    t = sys.stdout
    sys.stdout = archi
    help(range)
    sys.stdout = t

非常感谢您的帮助! 好的,我把到目前为止我从你们那里学到的东西放在一起了。 这会将我包中的所有内容打印到一个文本文件中。使其更易于阅读和搜索。我知道这有点骇人听闻。所以请随时改进它。再次感谢!!

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)

非常感谢您指出科波菲尔,这很有效。

tmp_list = dir(oSel)
content = '\n'.join(lines)
with open('C:/Users/Haggai/Desktop/dir.txt',"w") as text_file:
    text_file.write(content )