栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号