python-docx 在word中指定位置插入图片或表格

ops/2024/10/10 15:54:34/

docx库add_picture()方法不支持对图片位置的设置

1、新建一个1行3列的表格,在中间的一列中插入图片

python">word">from docx word">import Document
word">from docx.shared word">import Pt
word">from docx.oxml.shared word">import OxmlElement
word">from docx.enum.text word">import WD_ALIGN_PARAGRAPHword">def add_center_picture(self, image_path_or_stream, width=None, height=None):# run = self.doc.add_paragraph().add_run()tab = self.doc.add_table(rows=1, cols=3) # 添加一个1行3列的空表cell = tab.cell(0, 1) # 获取某单元格对象(从0开始索引)ph =cell.paragraphs[0]run = ph.add_run()# run.add_break()run.add_picture(image_path_or_stream, width=width, height=height)

2、在第一段右边加图片

python">word">from docx word">import Document
word">from docx.shared word">import Pt
word">from docx.oxml.shared word">import OxmlElement
word">from docx.enum.text word">import WD_ALIGN_PARAGRAPHword">def add_log_img(doc, log_path):# log_path : 图片本地地址doc.add_picture(log_path, width=Pt(100), height=Pt(100))doc.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHTword">return doc

3、指定位置(占位符替换)插入图片

  • 原模板文档(箭头处是占位符)
    在这里插入图片描述

  • 插入图片后
    在这里插入图片描述

python">word">from docx word">import Document
word">from docx.shared word">import Inches
word">from docx.oxml.ns word">import qn
word">from docx.enum.text word">import WD_ALIGN_PARAGRAPHword">def center_insert_img(doc, img):"""插入图片"""word">for paragraph word">in doc.paragraphs:# 根据文档中的占位符定位图片插入的位置word">if '<<img1>>' word">in paragraph.text:# 把占位符去掉paragraph.text = paragraph.text.replace('<<img1>>', '')# 添加一个文字块run = paragraph.add_run('')# 添加一个’回车换行效果‘run.add_break()# 添加图片并指定大小run.add_picture(img, width=Inches(6.2))word">def save_img_to_doc(img):"""把图片保存到doc文件中的指定位置"""tpl_doc = 'reports/template.docx'res_doc = 'reports/res/2022-03-11.docx'# 打开模板文件document = Document(tpl_doc)# 插入图片居中center_insert_img(document, img)# 保存结果文件document.save(res_doc)word">def main():"""主函数"""img = 'imgs/chart.png'save_img_to_doc(img)word">if __name__ == '__main__':main()
  • 占位符问题
    <<img1>>这个只是占位符,可以换成其他任何的,只要能唯一识别到,比如ph_img1。有时无法替换是因为程序中读取doc文件时格式问题没把占位符识别成一个,可能识别成了ph_和img1,这个时候你把占位符选中,剪切掉然后再粘贴,粘贴时选择只粘贴文字就行了。

4、在table的一个cell中插入图片

python">
word">from docx word">import Document 
word">from docx.shared word">import  Cm #引入cm单位,便于设置图片的宽度
word">from docx.enum.table word">import WD_TABLE_ALIGNMENT #用于设置单元格的内容居中对齐word">def insert_img2table():#创建文档document = Document()# 添加表格tab1 =document.add_table(rows=1,cols=1)   #添加一个1行1列的空表cell=tab1.cell(0,0)  #获取某单元格对象(从0开始索引)# 在单元格中添加段落c_p1 =cell.paragraphs[0]c_p1.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER #设置单元格内容居中对齐# 在单元格中添加区块c_run1=c_p1.add_run()# 在单元格(区块)中添加图片c_run1.add_picture('cat.png',width=Cm(10))word">return document 

pythondocxword_122">5、python使用docx向word文档中表格插入图片并固定缩放

使用python的docx模块对word文档进行编辑时,有时候需要向表格中插入图片,但是插入的图片一般是按照原图片的大小插入的,即使你的word文档一开始就设置好了固定宽高,似乎也是不起作用,这个时候就需要在插入后,用python去调整图片的宽高。

python">#向word文档中的第二个表格的第3行第3列插入了一个图片。
#然后获取当前图片的高度,将其宽度调整为固定的10.71cm,再然后通过计算宽度变化的比例,调整高度的变化。
# 最后将文档保存为一个新的docx文件即可
word">from docx word">import Document
word">from docx word">import shared# 本脚本用于测试word文件的表格写入document = Document("test.docx")#向word文档中的第二个表格的第3行第3列插入了一个图片。
pic = document.tables[1].cell(2,2).paragraphs[0].add_run().add_picture("Output_1.png")
#获取原图片的宽度
source_width = pic.width
#设置图片插入后的固定宽度
pic.width = shared.Cm(10.71)
#按图片宽度的缩放比例配置图片的高度
pic.height = int(pic.height * (pic.width / source_width))document.save("1.docx")

pythondocxword_147">6、通过python-docx给word文档中的指定位置添加表格

1.读取一个已有的word文档。docx格式。
2.在该word文档中,通过一个给定的文字。找到该位置。在该位置的下方添加一个表格。例如在图中“BUG情况表”的下方插入一个表格

6.1 需求

1.读取一个已有的word文档。docx格式。
2.在该word文档中,通过一个给定的文字。找到该位置。在该位置的下方添加一个表格。例如在图中“BUG情况表”的下方插入一个表格
在这里插入图片描述
3.表格内容如下。要求添加完该表格后,如果表格内容发生变更。还能再次通过该程序,修改表格里的数据。
在这里插入图片描述

6.2 设计
  • 通过python-docx读取word文档。通过document.paragraphs定位指定文字的位置。
  • 通过xlwings读取excel的内容,存成list[list[]]。
  • 通过docx的add_table增加一个表格,并且更改表头颜色,合并表格等操作
  • 通过识别表头的第一行,判断是否是已经存在这个表格,来决定是否要删除原表格
python"># -*- coding: UTF-8 -*-
word">import sys
word">from copy word">import deepcopy
word">import xlwings
word">from docx word">import Document
word">from docx.oxml.ns word">import nsdecls
word">from docx.oxml word">import parse_xml
word">def copy_table_after(table, paragraph):tbl, p = table._tbl, paragraph._pnew_tbl = deepcopy(tbl)p.addnext(new_tbl)
word">def move_table_after(table, paragraph):tbl, p = table._tbl, paragraph._pp.addnext(tbl)
word">def get_excel_date(filename):'''获得excel里的所有内容,返回list:param filename:  excel路径:return: list[list[]]'''app = xlwings.App(visible=False, add_book=True)app.display_alerts = Falseapp.screen_updating = Falsewb = app.books.open(filename)sht = wb.sheets[0]rng = sht.range('A1')# 把excel里的数据读取成 年-月-日 时:分:秒的格式my_date_handler = word">lambda year, month, day, hour, minute, second, **kwargs: "%04i-%02i-%02i %02i:%02i:%02i" % (year, month, day, hour, minute, second)# 取出所有内容,这里用ig这个变量,是为了庆祝I.G获得LOL S8赛季总冠军ig = rng.current_region.options(index=False, numbers=int, empty='N/A', dates=my_date_handler)result = ig.valuewb.close()app.quit()word">return result
word">def delete_table_with_title(document,expect_text):allTables = document.tablesword">for activeTable word">in allTables:word">if activeTable.cell(0, 0).paragraphs[0].text == expect_text:word">print('删除成功')activeTable._element.getparent().remove(activeTable._element)
word">def insert_table_after_text(file_name,excel_name,expect_text):document = Document(file_name)# 因为docx读出来的都是unicode类型的,所以我们要用unicode类型的进行查找expect_text=expect_text.decode('utf-8')delete_table_with_title(document,expect_text)target = Noneword">for paragraph word">in document.paragraphs:paragraph_text = paragraph.textword">if paragraph_text.endswith(expect_text):target = paragraphword">breakword">if target word">is word">not None:records = get_excel_date(excel_name)# 获得excel数据的栏数,初始化一个空的tablecol = len(records[0])table = document.add_table(rows=1, cols=col)table.style = 'Table Grid'# 给table加一个表头,并且合并第一栏shading_elm_1 = parse_xml(r'<w:shd {} w:fill="D9E2F3"/>'.format(nsdecls('w')))table.rows[0].cells[0]._tc.get_or_add_tcPr().append(shading_elm_1)table.rows[0].cells[0].text=expect_texttable_row=table.rows[0]first=table_row.cells[0]end=table_row.cells[-1]first.merge(end)# 合并结束,开始把excel里的内容添加到table里word">for tr_list word">in records:row_cells = table.add_row().cellsindex = 0word">for td_list word">in tr_list:row_cells[index].text = td_listindex = index + 1# 把添加的table移动到指定的位置move_table_after(table, target)# 保存document.save(file_name)
word">if __name__ == '__main__':insert_table_after_text('demo2.docx', 'demo.xlsx',"BUG情况表")

最终效果:

在这里插入图片描述


参考

官方文档
github/python-docx
python-docx生成word,插入图片居中显示问题解决办法
python-docx替换字符串【保存原有样式
Python在word的指定位置插入图片
feature: _Cell.add_picture() #10
python使用docx向word文档中表格插入图片并固定缩放
python如何在docx的指定位置插入图片?
通过python-docx给word文档中的指定位置添加表格
python-docx中文


http://www.ppmy.cn/ops/43927.html

相关文章

LeetCode 题解:112. 路径总和,递归,JavaScript,详细注释

原题链接&#xff1a; 112. 路径总和 解题思路&#xff1a; 如果求根节点到叶子节点的路径上的节点值之和&#xff0c;假设共有3个节点&#xff0c;那么写成计算式是val1 val2 val3 sum那么将计算式转换就可以得到val3 sum - val1 - val2也就是说&#xff0c;问题可以从…

PyTorch Conv2d 前向传递中发生了什么?

在PyTorch中,nn.Conv2d 是一个用于二维卷积操作的模块。当你对一个输入张量(通常是一个四维的,形状为 [batch_size, channels, height, width])调用 forward 方法(或直接使用模块作为函数调用)时,Conv2d 会执行一系列的计算来产生输出张量。 以下是 Conv2d 前向传递中发…

NIO的ByteBuffer和Netty的ByteBuf的性能

在讨论Java NIO的ByteBuffer与Netty的ByteBuf的性能时&#xff0c;需要考虑几个主要的因素&#xff0c;因为性能表现并不是绝对的&#xff0c;而是依赖于具体的使用场景。Netty的ByteBuf设计更加现代&#xff0c;针对网络编程的需求进行了优化&#xff0c;包含了许多ByteBuffer…

C++ RPC ORM 高速解析

支持所有常用编程语 https://capnproto.org/GitHub - capnproto/capnproto: Capn Proto serialization/RPC system - core tools and C library https://capnproto.org/capnproto-c-win32-1.0.2.zip 常用命令&#xff1a; capnp help capnp compile -oc myschema.capn…

LeeCode 3165 线段树

题意 传送门 LeeCode 3165 不包含相邻元素的子序列的最大和 题解 考虑不含相邻子序列的最大和&#xff0c;在不带修改的情况下容易想到&#xff0c;以最后一个元素是否被选取为状态进行DP。从线性递推的角度难以处理待修改的情况。 从分治的角度考虑&#xff0c;使用线段树…

Mac 安装 git

文章目录 前言一、介绍二、下载三、验证四、配置五、Git常用命令六、git提交和撤销工作流程代码提交和提交同步代码撤销和撤销同步 FAQ1.homebrew 下载解决方法一&#xff08;强烈推荐&#xff09;&#xff1a;解决方法二&#xff1a; 总结 前言 Git 是一个开源的分布式版本控…

NLP(18)--大模型发展(2)

前言 仅记录学习过程&#xff0c;有问题欢迎讨论 Transformer结构&#xff1a; LLM的结构变化&#xff1a; Muti-head 共享&#xff1a; Q继续切割为muti-head,但是K,V少切&#xff0c;比如切为2个&#xff0c;然后复制到n个muti-head减少参数量&#xff0c;加速训练 atte…