当前位置:网站首页>Batch replace tables in Word with pictures and save
Batch replace tables in Word with pictures and save
2022-08-01 13:04:00 【Xiaoming - code entity】
之前我在《将WordThe table in the picture is copied to WeChat and sent》Shared how to assignwordThe table in is pasted as a picture,参考:https://xxmdmst.blog.csdn.net/article/details/125999841
Today, a group of friends asked how to make oneWordReplace all tables with pictures,Please see my complete code below:
import os
import win32com.client as win32
from io import BytesIO
import win32clipboard
from PIL import Image, ImageGrab, ImageChops
def crop_image(im, backgroud_color=(255, 255, 255)):
'''Crop the margins of the image and fill the alpha channel with a white background,作者:小小明'''
bg = Image.new(mode='RGBA', size=im.size)
bbox = ImageChops.difference(im, bg).getbbox()
if bbox:
im = im.crop(bbox)
img = Image.new('RGB', im.size, backgroud_color)
img.paste(im, (0, 0, *im.size), im)
return img
def write_img2clipboard(img):
"""Write the specified picture to the clipboard,作者:小小明"""
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
out = BytesIO()
img.save(out, "BMP")
# BMP图片有14字节的header,need to be removed
data = out.getvalue()[14:]
# DIB: 设备无关位图(device-independent bitmap)
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
finally:
win32clipboard.CloseClipboard()
def process_clipboard_pic(backgroud_color=(255, 255, 255)):
"""Blank the clipping edges of the image in the clipboard and add a background color to the transparent parts,作者:小小明"""
img = ImageGrab.grabclipboard()
img = crop_image(img, backgroud_color)
write_img2clipboard(img)
return img
# 获取正在运行的Word应用
wordApp = win32.GetActiveObject("Word.Application")
# 获取当前激活的word文档
doc = wordApp.ActiveDocument
# Iterate over all table objects
for t in doc.Tables:
t.Select()
wordApp.Selection.CopyAsPicture()
img = process_clipboard_pic()
# display(img)
t.Delete()
wordApp.Selection.Paste()
# 另存为word文件
doc.SaveAs2(os.path.abspath("替换图片.docx"))
As long as we open the ones that need to be replaced in batchesWord文件,然后执行上述代码,You can see the effect before and after running.
打开的Word文档:

运行上述代码后:

The table can be replaced smoothly,If you need to change the background color of the picture,可以向process_clipboard_pic()Pass in the specified color value,Such as bean paste color background:
process_clipboard_pic(backgroud_color=(204, 232, 207))
There are also some problems when a table grows to span multiple pages,The program can only take screenshots to the content of the first page,This requires that long tables must be manually split in advance.
我使用WPS's split by row feature:

Run the above code again after manually splitting the table,The complete table image has been successfully captured:

注意:Save as if not neededWord文档,可以直接保存,The last line of code can be modified as follows:
doc.Save()Of course you can also delete the last line of code about saving,Manually review the modification results,Decide whether to save or save as.
It is recommended to save as a backup of the original document as well,In case which form is too long and forget to split it?
边栏推荐
- 8. SAP ABAP OData 服务如何支持创建(Create)操作
- 找出相同属性值的对象 累加数量 汇总
- leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
- 计算器:中缀表达式转后缀表达式
- Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
- JMP Pro 16.0 software installation package download and installation tutorial
- 故障007:dexp导数莫名中断
- Find objects with the same property value Cumulative number Summarize
- 观察者模式
- How to integrate 3rd party service center registration into Istio?
猜你喜欢

快速幂---学习笔记

安全又省钱,“15岁”老小区用上管道燃气

bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)

NebulaGraph v3.2.0 Performance Report

uniapp读取和写入文件

Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing

Dameng replaces the officially authorized dm.key

关于Request复用的那点破事儿。研究明白了,给你汇报一下。

JMP Pro 16.0 software installation package download and installation tutorial

数字孪生北京故宫,元宇宙推进旅游业进程
随机推荐
六石编程学:问题要面对,办法要技巧,做不好的功能要想办法
脚本语言Lua的基础知识总结
SQL函数 %SQLSTRING
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
人像分割技术解析与应用
MMF的初步介绍:一个规范化的视觉-语言多模态任务框架
《MySQL核心知识》第6章:查询语句
Do wildcard SSL certificates not support multiple domains?
How to successfully pass the CKA exam?
阿里云官方 Redis 开发规范
什么是一致性哈希?可以应用在哪些场景?
AI目标分割能力,无需绿幕即可实现快速视频抠图
SQL function SQRT
并发编程10大坑,你踩过几个?
小程序插件如何帮助开发者受益?
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
SQL functions STR
MVVM响应式
formatdatetime function mysql (date sub function)
SQL函数 STR