当前位置:网站首页>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?
边栏推荐
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- 安装apex报错
- Aeraki Mesh Joins CNCF Cloud Native Panorama
- 如何设计一个分布式 ID 发号器?
- JMP Pro 16.0软件安装包下载及安装教程
- 初级必备:单例模式的7个问题
- 数据挖掘-03
- Istio Meetup China: Full Stack Service Mesh - Aeraki Helps You Manage Any Layer 7 Traffic in an Istio Service Mesh
- How to get the address of WeChat video account (link address of WeChat public account)
- LeetCode_动态规划_中等_313.超级丑数
猜你喜欢

什么是一致性哈希?可以应用在哪些场景?

小程序插件如何帮助开发者受益?

shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))

CloudCompare&PCL ICP配准(点到面)

数字孪生北京故宫,元宇宙推进旅游业进程

Feign 从注册到调用原理分析

VINS-mono 论文解读:IMU预积分+Marg边缘化

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

Process sibling data into tree data

如何使用 Authing 单点登录,集成 Discourse 论坛?
随机推荐
重磅消息 | Authing 实现与西门子低代码平台的集成
【2022蓝帽杯】file_session && 浅入opcode
为什么最大值加一等于最小值
AI目标分割能力,无需绿幕即可实现快速视频抠图
[Community Star Selection] Issue 24 August Update Plan | Keep writing, refuse to lie down!More original incentive packages, as well as Huawei WATCH FIT watches!
Data frame and remote frame of CAN communication
AI目标分割能力,无需绿幕即可实现快速视频抠图
深入解析volatile关键字
How to integrate 3rd party service center registration into Istio?
批量任务导入到数据库中
如何使用 Authing 单点登录,集成 Discourse 论坛?
Apex installation error
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
How does the SAP ABAP OData service support the Create operation trial version
The CAN communication standard frame and extended frame is introduced
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
Why does the maximum plus one equal the minimum
uniapp读取和写入文件
英特尔全方位打造算力基础,助推“算”赋百业
那些利用假期学习的职场人,后来都怎么样了?