当前位置: 首页 > news >正文

python替换word文件中的图片

python替换word文件中的图片

模拟鼠标键盘,截屏

import glob
import os
import timeimport 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文件中的图片

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_partwith 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')
http://www.lryc.cn/news/371081.html

相关文章:

  • Servlet-01
  • C语言:链表
  • 【git使用二】gitee远程仓库创建与本地git命令用法
  • 明星百科大全PHP网站源码
  • 白酒:茅台镇白酒的品鉴会与文化交流活动
  • python中列表结构在点云数据处理中用法
  • 土耳其(小亚细亚)历史上的各个阶段
  • Windows下基于Frida查看内存基址和修改寄存器
  • 2024中国网络安全产品用户调查报告(发布版)
  • 手写图片懒加载
  • 大型语言模型(LLMs)的后门攻击和防御技术
  • 力扣2594.修车的最少时间
  • 攻防演练之-成功的钓鱼邮件溯源
  • Gi标签管理
  • 2024福建等保测评公司有哪些?分别叫做什么名字?
  • 王先宏老师厉害了,活页笔记版古琴曲谱拆箱图
  • TalkingData 是一家专注于提供数据统计和分析解决方案的独立第三方数据智能服务平台
  • Springboot的小型超市商品展销系统-计算机毕业设计源码01635
  • UV胶开裂主要因素有哪些?如何避免?
  • LogicFlow 学习笔记——3. LogicFlow 基础 节点 Node
  • VMware清理拖拽缓存
  • 跨语言系统中的功能通信:Rust、Java、Go和C++的最佳实践
  • 4. Revit API UI 之 Ribbon(界面)
  • js数组方法
  • PyTorch -- 最常见损失函数 LOSS 的选择
  • Prometheus 监控系统
  • Spring Boot中使用logback出现LOG_PATH_IS_UNDEFINED文件夹
  • 代码随想录——组合总数Ⅲ(Leetcode216)
  • Android native层的线程分析(C++),以及堆栈打印调试
  • 计算机科学:2024年高考生的明智之选?兴趣与趋势并重的决策指南