docx库add_picture()方法不支持对图片位置的设置
1、新建表格
新建一个1行3列的表格,在中间的一列中插入图片
from docx import document from docx.shared import pt from docx.oxml.shared import oxmlelement from docx.enum.text import wd_align_paragraph 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、在第一段右边加图片
from docx import document from docx.shared import pt from docx.oxml.shared import oxmlelement from docx.enum.text import wd_align_paragraph 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.right return doc
3、指定位置(占位符替换)插入图片
原模板文档(箭头处是占位符)
插入图片后
from docx import document from docx.shared import inches from docx.oxml.ns import qn from docx.enum.text import wd_align_paragraph def center_insert_img(doc, img): """插入图片""" for paragraph in doc.paragraphs: # 根据文档中的占位符定位图片插入的位置 if '<<img1>>' in paragraph.text: # 把占位符去掉 paragraph.text = paragraph.text.replace('<<img1>>', '') # 添加一个文字块 run = paragraph.add_run('') # 添加一个'回车换行效果‘ run.add_break() # 添加图片并指定大小 run.add_picture(img, width=inches(6.2)) 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) def main(): """主函数""" img = 'imgs/chart.png' save_img_to_doc(img) if __name__ == '__main__': main()
占位符问题
<<img1>>这个只是占位符,可以换成其他任何的,只要能唯一识别到,比如ph_img1。有时无法替换是因为程序中读取doc文件时格式问题没把占位符识别成一个,可能识别成了ph_和img1,这个时候你把占位符选中,剪切掉然后再粘贴,粘贴时选择只粘贴文字就行了。
4、在table的一个cell中插入图片
from docx import document from docx.shared import cm #引入cm单位,便于设置图片的宽度 from docx.enum.table import wd_table_alignment #用于设置单元格的内容居中对齐 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)) return document
5、python使用docx向word文档中表格插入图片并固定缩放
使用python的docx模块对word文档进行编辑时,有时候需要向表格中插入图片,但是插入的图片一般是按照原图片的大小插入的,即使你的word文档一开始就设置好了固定宽高,似乎也是不起作用,这个时候就需要在插入后,用python去调整图片的宽高。
#向word文档中的第二个表格的第3行第3列插入了一个图片。 #然后获取当前图片的高度,将其宽度调整为固定的10.71cm,再然后通过计算宽度变化的比例,调整高度的变化。 # 最后将文档保存为一个新的docx文件即可 from docx import document from docx 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")
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增加一个表格,并且更改表头颜色,合并表格等操作
通过识别表头的第一行,判断是否是已经存在这个表格,来决定是否要删除原表格
# -*- coding: utf-8 -*- import sys from copy import deepcopy import xlwings from docx import document from docx.oxml.ns import nsdecls from docx.oxml import parse_xml def copy_table_after(table, paragraph): tbl, p = table._tbl, paragraph._p new_tbl = deepcopy(tbl) p.addnext(new_tbl) def move_table_after(table, paragraph): tbl, p = table._tbl, paragraph._p p.addnext(tbl) def get_excel_date(filename): ''' 获得excel里的所有内容,返回list :param filename: excel路径 :return: list[list[]] ''' app = xlwings.app(visible=false, add_book=true) app.display_alerts = false app.screen_updating = false wb = app.books.open(filename) sht = wb.sheets[0] rng = sht.range('a1') # 把excel里的数据读取成 年-月-日 时:分:秒的格式 my_date_handler = 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.value wb.close() app.quit() return result def delete_table_with_title(document,expect_text): alltables = document.tables for activetable in alltables: if activetable.cell(0, 0).paragraphs[0].text == expect_text: print('删除成功') activetable._element.getparent().remove(activetable._element) 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 = none for paragraph in document.paragraphs: paragraph_text = paragraph.text if paragraph_text.endswith(expect_text): target = paragraph break if target is not none: records = get_excel_date(excel_name) # 获得excel数据的栏数,初始化一个空的table col = 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_text table_row=table.rows[0] first=table_row.cells[0] end=table_row.cells[-1] first.merge(end) # 合并结束,开始把excel里的内容添加到table里 for tr_list in records: row_cells = table.add_row().cells index = 0 for td_list in tr_list: row_cells[index].text = td_list index = index + 1 # 把添加的table移动到指定的位置 move_table_after(table, target) # 保存 document.save(file_name) if __name__ == '__main__': insert_table_after_text('demo2.docx', 'demo.xlsx',"bug情况表")
最终效果:
到此这篇关于python实现在word中指定位置插入图片或表格的文章就介绍到这了,更多相关python word插入图片或表格内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论