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', '光大理财')