当前位置:网站首页>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?
边栏推荐
- 达梦更换正式授权dm.key
- This article will take you to thoroughly clarify the working mechanism of certificates in Isito
- 8. How does the SAP ABAP OData service support the Create operation
- 为什么最大值加一等于最小值
- shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
- JMP Pro 16.0 software installation package download and installation tutorial
- 批量任务导入到数据库中
- 芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
- 计算器:中缀表达式转后缀表达式
- 通讯录(静态版)(C语言)(VS)
猜你喜欢
Fault 007: The dexp derivative is inexplicably interrupted
力扣160题,相交链表
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
The basic knowledge of scripting language Lua summary
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
程序员的自我修养
How does the SAP ABAP OData service support the Create operation trial version
CloudCompare & PCL ICP registration (point to face)
数字孪生北京故宫,元宇宙推进旅游业进程
这项工作事关中小学生生命安全!五部门作出联合部署
随机推荐
如何将第三方服务中心注册集成到 Istio ?
How does the SAP ABAP OData service support the Create operation trial version
将同级数据处理成树形数据
Istio投入生产的障碍以及如何解决这些问题
【2022蓝帽杯】file_session && 浅入opcode
CloudCompare & PCL ICP registration (point to face)
多线程案例——定时器
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
通讯录(静态版)(C语言)(VS)
故障007:dexp导数莫名中断
How to Integrate Your Service Registry with Istio?
How do we do full-link grayscale on the database?
快速幂---学习笔记
线上问题排查常用命令,总结太全了,建议收藏!!
uniapp读取和写入文件
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
找出相同属性值的对象 累加数量 汇总
【讲座分享】“营收“看金融
LeetCode_位运算_简单_405.数字转换为十六进制数
如何使用 Authing 单点登录,集成 Discourse 论坛?