python使用docxtpl庫(kù)實(shí)現(xiàn)圖片替換功能
代碼示例
docxtpl 一個(gè)很強(qiáng)大的包,其主要通過(guò)對(duì)docx文檔模板加載,從而對(duì)其進(jìn)行修改,我主要是用docxtpl對(duì)圖片進(jìn)行替換。
簡(jiǎn)單代碼如下:
import base64
from docxtpl import DocxTemplate
pl = int(input('輸出數(shù)字:'))
num = 'D:\\py\\testdata\\1.docx'
tpl = DocxTemplate(num)
# 定義圖片名稱位置
context = {
1: '1.jpg',
2: '2.jpg',
}
images = "base64"
tmp_img = "%s.jpg" % (pl)
with open(tmp_img, 'wb') as image_tpl:
data = images.split(',')
imgs_tmp = base64.b64decode(data[1])
image_tpl.write(imgs_tmp)
datas = image_tpl.name
print(datas)
# 判斷位置,進(jìn)行圖片替換
if tpl:
tpl.replace_pic(context.get(pl), datas)
print(tpl)
tpl.save("決定1003.docx")
print(tpl)查找圖片的位置
如果圖片位置不清楚可以使用docx轉(zhuǎn)xml,然后進(jìn)行解壓在目錄word\document.xml下面可以找到圖片的位置。


方法補(bǔ)充
1.Python替換圖片的指定區(qū)域
要在Python中替換圖片的指定區(qū)域,可以使用Pillow庫(kù)。以下是一個(gè)簡(jiǎn)單的例子,展示如何替換圖片的一個(gè)區(qū)域:
from PIL import Image
def replace_image_region(src_path, dest_path, region, replacement_image):
# 加載原始圖片和替換區(qū)域的圖片
image = Image.open(src_path)
replacement = Image.open(replacement_image).convert(image.mode)
# 獲取替換區(qū)域的大小
region_width, region_height = region[2] - region[0], region[3] - region[1]
# 調(diào)整替換圖片的大小以匹配替換區(qū)域
replacement = replacement.resize((region_width, region_height))
# 通過(guò)掩碼獲取替換區(qū)域
mask = Image.new("L", (region_width, region_height), 255)
region_image = image.crop(region)
# 應(yīng)用掩碼和替換圖片
region_image.paste(replacement, (0, 0), mask)
# 粘貼圖片區(qū)域回原圖
image.paste(region_image, region)
# 保存新圖片
image.save(dest_path)使用示例
src_img_path = ‘source.jpg' # 原始圖片路徑 dest_img_path = ‘destination.jpg' # 替換后保存的圖片路徑 replacement_img_path = ‘replacement.png' # 替換區(qū)域的圖片路徑 region = (100, 100, 300, 300) # 替換的區(qū)域坐標(biāo) (左, 上, 右, 下) replace_image_region(src_img_path, dest_img_path, region, replacement_img_path)
2.python-docx替換Word中圖片的方法
需要先安裝python-docx:
pip install python-docx
再使用以下的代碼:
import docx
from docx.shared import Cm
def replace_img(in_word_path, out_word_path, output_paragraph_idx, new_img_path, height, width):
"""
Replace a image in a docx(Word) file.
:param in_word_path: The path of the input docx file to be replaced
:param out_word_path: The path of the output docx file
:param new_img_path: The path of the image that will be the new image in the docx file(i.e. one image(The image is assigned by output_paragraph_idx) will be replaced by the image which is in img_path)
:param output_paragraph_idx: The paragraph index of the image in the docx file(The index starts with 0)
:param height: The height of the new image which is in centimeters.
:param width: The width of the new image which is in centimeters..
:return: Empty
"""
doc = docx.Document(in_word_path)
para = doc.paragraphs[output_paragraph_idx]
para.clear()
pic = para.add_run().add_picture(new_img_path)
pic.height = Cm(height)
pic.width = Cm(width)
doc.save(out_word_path)
到此這篇關(guān)于python使用docxtpl庫(kù)實(shí)現(xiàn)圖片替換功能的文章就介紹到這了,更多相關(guān)python docxtpl圖片替換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用Opencv打開(kāi)筆記本電腦攝像頭報(bào)錯(cuò)解問(wèn)題及解決
這篇文章主要介紹了Python使用Opencv打開(kāi)筆記本電腦攝像頭報(bào)錯(cuò)解問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
turtle的基礎(chǔ)使用之python?turtle遞歸繪圖
這篇文章主要介紹了turtle的基礎(chǔ)使用之python?turtle遞歸繪圖,turtle是一種比較簡(jiǎn)單的第三方庫(kù),下面借助遞歸繪圖詳細(xì)描述該內(nèi)容,具有一的的知識(shí)性參考價(jià)值,需要的朋友可以參考一下2022-02-02
python數(shù)據(jù)可視化matplotlib繪制折線圖示例
這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python神經(jīng)網(wǎng)絡(luò)Keras構(gòu)建CNN網(wǎng)絡(luò)訓(xùn)練
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)使用Keras構(gòu)建CNN網(wǎng)絡(luò)訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python實(shí)現(xiàn)自動(dòng)玩連連看的腳本分享
最近女朋友在玩連連看,玩了一個(gè)星期了還沒(méi)通關(guān),真的是菜。實(shí)在是看不過(guò)去了,直接用python寫了個(gè)腳本代碼可以自動(dòng)玩連連看,感興趣的可以了解一下2022-04-04
Python+streamlit實(shí)現(xiàn)輕松創(chuàng)建人事系統(tǒng)
streamlit 是 基于 Python 的一個(gè)非常強(qiáng)大的 web 構(gòu)建系統(tǒng),通過(guò)該類庫(kù),我們可以實(shí)現(xiàn)不需要編寫一行前端代碼而構(gòu)建一個(gè)完整的 Web 應(yīng)用。下面我們就來(lái)編寫一個(gè)簡(jiǎn)單的人事系統(tǒng)吧2023-02-02
Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承簡(jiǎn)單示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承,結(jié)合完整實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義、調(diào)用、繼承及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03

