https://python-docx.readthedocs.io/en/latest/_modules/docx/text/paragraph.html?highlight=add_run
需要用到一个add_run方法,用法见以上文档。 我的代码片段:新建文档对象,即实例化Document类,实现将15*2张图片放入15行2列的表格中,保存为a.docx
def getOutputtoWord(**kwargs):
if len(kwargs)==1:
check_index=kwargs['check_index']
check_img = Image.open('./gene_9000/{}.jpg'.format(check_index))
check_img = np.array(check_img)
print(-(check_img/255.0))
check_img = -(check_img/255.0) + 1
print(check_img)
plt.imshow(check_img, cmap='Greys_r')
orig_str='./output/orig{}__{}.jpg'.format(check_index,time.strftime("_%a_%b_%d_%H_%M_%S_%Y", time.localtime()))
plt.savefig(orig_str)
check_img = check_img[None, :, :, 0]
_, _, check_code = VAER_GAN_instance.encoder.predict(check_img)
print(check_code)
plt.imshow(np.squeeze(VAER_GAN_instance.generator.predict(
check_code)), cmap='Greys_r')
output_str='./output/output{}__{}.jpg'.format(check_index,time.strftime("_%a_%b_%d_%H_%M_%S_%Y", time.localtime()))
plt.savefig(output_str)
elif len(kwargs)==2:
check_index=kwargs['check_index']
cellid=kwargs['cell_id']
check_index=kwargs['check_index']
check_img = Image.open('./gene_9000/{}.jpg'.format(check_index))
check_img = np.array(check_img)
print(-(check_img/255.0))
check_img = -(check_img/255.0) + 1
print(check_img)
plt.imshow(check_img, cmap='Greys_r')
plt.margins(0,0)
plt.axis('off')
orig_str='./output/orig{}__{}.jpg'.format(check_index,time.strftime("_%a_%b_%d_%H_%M_%S_%Y", time.localtime()))
plt.savefig(orig_str,bbox_inches = 'tight',pad_inches = 0)
orig_str=os.path.abspath(orig_str)
cell=tab.cell(cellid,0)
ph =cell.paragraphs[0]
run=ph.add_run()
run.add_picture(orig_str)
check_img = check_img[None, :, :, 0]
_, _, check_code = VAER_GAN_instance.encoder.predict(check_img)
print(check_code)
plt.margins(0,0)
plt.imshow(np.squeeze(VAER_GAN_instance.generator.predict(
check_code)), cmap='Greys_r')
plt.margins(0,0)
plt.axis('off')
output_str='./output/output{}__{}.jpg'.format(check_index,time.strftime("_%a_%b_%d_%H_%M_%S_%Y", time.localtime()))
plt.savefig(output_str,bbox_inches = 'tight',pad_inches = 0)
cell=tab.cell(cellid,1)
ph =cell.paragraphs[0]
run=ph.add_run()
output_str=os.path.abspath(output_str)
run.add_picture(output_str)
|