python替换word文件中的图片

python替换word文件中的图片

模拟鼠标键盘,截屏

python 复制代码
import glob
import os
import time

import pyautogui
import pyautogui as p
from PIL import ImageGrab
from pynput.keyboard import Controller

# -*- coding:utf-8 -*-


directory = './'

directory1 = './output'

for f in glob.glob(os.path.join(directory, '*.docx')):
    os.remove(f)

for f in glob.glob(os.path.join(directory1, '*.png')):
    os.remove(f)


def find_and_click(image_path, confidence):
    image_location = pyautogui.locateOnScreen(image_path, grayscale=True, confidence=confidence)
    if image_location:
        time.sleep(0.5)
        position = pyautogui.center(image_location)
        pyautogui.click(position)
    else:
        print(f"{image_path} 未找到指定的图片")


time.sleep(1)

# 初始化键盘控制器i
keyboard = Controller()

find_and_click("./images/01hf.jpg", 0.8)
time.sleep(1)

find_and_click("./images/02my.jpg", 0.8)
time.sleep(1)

find_and_click("./images/03bg.jpg", 0.8)
time.sleep(1)

find_and_click("./images/04cty.jpg", 0.8)
time.sleep(1)

find_and_click("./images/05zsxx.jpg", 0.8)
time.sleep(3)

find_and_click("./images/06sj.jpg", 0.8)
time.sleep(2)

find_and_click("./images/07ss.jpg", 0.8)
time.sleep(2)

keyboard = Controller()

keyboard.press(key='p')  # 按键按下s
time.sleep(0.2)
keyboard.release(key='p')  # 按键松开s
time.sleep(1)

find_and_click("./images/08sj.jpg", 0.8)
time.sleep(1)

p.doubleClick(1169, 145, 0.5)

time.sleep(5)

# 定义截图区域的左上角和右下角坐标
# 这里的坐标应根据你的需求进行调整
left, top, right, bottom = 992, 217, 1899, 391

# 使用ImageGrab.grab()方法进行截图
# 参数为截图区域的左上角和右下角坐标
screenshot = ImageGrab.grab(bbox=(left, top, right, bottom))

# 保存截图图片
screenshot.save('./output/0test.png')

time.sleep(0.5)
find_and_click("./images/14ht.jpg", 0.8)
time.sleep(3)

python替换word文件中的图片

python 复制代码
import glob
import os
import shutil
from docx import Document

# -*- coding:utf-8 -*-

# 删除目录下所有的*.docx文件
directory = './'

for f in glob.glob(os.path.join(directory, '*.docx')):
    os.remove(f)

# 将源文件拷贝到目标目录
source_file = 'D:\\周报整理\\2024年周报\\2024年4月运维服务报告v06.docx'
target_directory = './'
shutil.copy(source_file, target_directory)

# 获取目录下所有文件
folder_path = r'./'

file_list = os.listdir(folder_path)

# 遍历文件列表,修改xxx.docx改名为test.docx
for file_name in file_list:
    # 检查文件是否以 .xls 结尾
    if file_name.endswith('.docx'):
        # 构造新文件名
        new_file_name = os.path.join(folder_path, 'test.docx')
        # 重命名文件
        os.rename(os.path.join(folder_path, file_name), new_file_name)


def get_image_info(doc):
    for rel in doc.part.rels.values():
        if rel.reltype == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image':
            print('Image ID:', rel.rId, 'Filename:', rel.target_part.filename)


def replace_image_by_id(doc, rId, new_image_path):
    for rel in doc.part.rels.values():
        if rel.reltype == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image' and rel.rId == rId:
            target_part = rel.target_part
            with open(new_image_path, 'rb') as f:
                target_part._blob = f.read()


document = Document('test.docx')

# 获取图片信息
get_image_info(document)

# 替换图片
replace_image_by_id(document, 'rId11', './output/0test.png')


# 保存文档
document.save('output_test.docx')
相关推荐
weixin_307779133 分钟前
批量OCR的GitHub项目
python·github·ocr
Evand J10 分钟前
【MATLAB例程】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,附下载链接
开发语言·matlab
机器视觉知识推荐、就业指导10 分钟前
Qt 与Halcon联合开发八: 结合Qt与Halcon实现海康相机采图显示(附源码)
开发语言·数码相机·qt
Heartoxx38 分钟前
c语言-指针与一维数组
c语言·开发语言·算法
hqxstudying40 分钟前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
charlie1145141911 小时前
如何使用Qt创建一个浮在MainWindow上的滑动小Panel
开发语言·c++·qt·界面设计
孤狼warrior1 小时前
灰色预测模型
人工智能·python·算法·数学建模
神仙别闹1 小时前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心2 小时前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金2 小时前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python