代码部分如下:
for j, row in enumerate(data):
for datum in row:
line_height = lh_list[j] # choose right height for current row
if datum is row[0]:
pdf.set_font("Times", 'B', size=8)
pdf.multi_cell(col_width_name, line_height, datum, border=1, align='L', ln=3,
max_line_height=pdf.font_size, fill=True)
else:
pdf.set_font("Times", size=8)
pdf.multi_cell(col_width_value, line_height, datum, border=1, align='C', ln=3,
max_line_height=pdf.font_size)
pdf.ln(line_height)
它工作得很好,但由于multi_cell()方法在控制台中有打印。是否有可能静音/关闭打印?
通过添加以下代码解析:
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout