import win32com
from win32com.client import Dispatch
import os,time
def getWordAddWatermark(file_path, context):
"""
:param file_path: 文件的路径
:param context: 水印的内容
:return:
"""
wordApp = win32com.client.DispatchEx("Word.Application") # 打开word进程
wordApp.Visible = True
# wordApp.DisplayAlerts = False
doc = wordApp.Documents.Open(file_path)
actDoc = wordApp.ActiveDocument
Sect = actDoc.Sections(1).Range.Select()
wordApp.ActiveWindow.ActivePane.View.SeekView = 9
wordApp.Selection.HeaderFooter.Shapes.AddTextEffect(0, context, "等线", 1, False, False, 0, 0).Select()
# wordApp.Selection.ShapeRange.Name = '0'
wordApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
wordApp.Selection.ShapeRange.Line.Visible = False
wordApp.Selection.ShapeRange.Fill.Visible = True
wordApp.Selection.ShapeRange.Fill.Solid
# wordApp.Selection.ShapeRange.Fill.Transparency = 0.5
# 设置颜色
wordApp.Selection.ShapeRange.Fill.ForeColor.ObjectThemeColor = 0
wordApp.Selection.ShapeRange.Rotation = 315
wordApp.Selection.ShapeRange.LockAspectRatio = True
wordApp.Selection.ShapeRange.Height = 120
wordApp.Selection.ShapeRange.Width = 320
wordApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
wordApp.Selection.ShapeRange.WrapFormat.Type = 3
wordApp.Selection.ShapeRange.WrapFormat.Side = 3
wordApp.Selection.ShapeRange.RelativeVerticalPosition = 0
wordApp.Selection.ShapeRange.Left = -999995
wordApp.Selection.ShapeRange.Top = -999995
# 关闭页眉页脚
wordApp.ActiveWindow.ActivePane.View.SeekView = 0
actDoc.Save()
actDoc.Close()
os.system("taskkill /f /im WINWORD.EXE")
if __name__ == '__main__':
getWordAddWatermark(r'C:\Users\Administrator\Desktop\需求\产品信息日报表需求\cc.docx', '光大理财')
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤