欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

python-docs插入表格,表格里放图片的办法

时间:2023-06-01

https://python-docx.readthedocs.io/en/latest/_modules/docx/text/paragraph.html?highlight=add_run#

需要用到一个add_run方法,用法见以上文档。
下面是我的代码片段,这里放给大家:
新建文档对象,即实例化document类,实现将15*2张图片放入15行2列的表格中,保存为a.docx(一个docx文件)
而下面的代码片段做的事就是将某张指定的图片放入excel指定的位置,即长度为15的大循环中的循环体。

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) # plt.show() check_img = check_img[None, :, :, 0] # print(check_img.shape) _, _, 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) # sht.pictures.add(orig_str,left=sht.range(cell).left, top=sht.range(cell).top,width=check_img.shape[0],height=check_img.shape[1]) 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)

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。