使用Python docx2pdf时如何屏蔽输出的HBox进度条相关提示信息
解决方案
方法1:使用库原生参数(推荐,适用于docx2pdf 0.1.8及以上版本)
convert 方法原生提供了disable_progress_bar参数,直接设为True即可关闭进度条输出:
from docx2pdf import convert convert(path, disable_progress_bar=True)
方法2:临时重定向标准输出(兼容所有版本)
如果你的docx2pdf版本较低没有上述参数,可以通过临时重定向标准输出的方式吞掉所有打印内容:
from docx2pdf import convert from contextlib import redirect_stdout import io # with块内的所有标准输出都会被暂存到内存中,不会打印到控制台 with redirect_stdout(io.StringIO()): convert(path)
如果上述方法仍有残留输出,可以同时重定向标准错误流:
from docx2pdf import convert from contextlib import redirect_stdout, redirect_stderr import io with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): convert(path)
内容的提问来源于stack exchange,提问作者ASE
相关产品推荐
相关产品推荐

