python-添加页眉,页脚,水印

python-添加页眉,页脚,水印

import json
import osimport win32com
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt, Inches, Cm
from win32com.client import Dispatchclass DocReport(object):def __init__(self, **kwargs):self.file = kwargs.get("file", None)if self.file is None:self.file = os.path.abspath("report.docx")self.document = Document()def addHeader(self, **kwargs):"""添加页眉:return:"""# document 对象document = self.documenttext = kwargs.get("text", None)image = kwargs.get("image", None)section = document.sections[0]header = section.headerif text is not None:paragraph = header.paragraphs[0]run = paragraph.add_run()run.add_text(text)if image is not None:paragraph = header.paragraphs[0]run = paragraph.add_run()run.add_picture(image)def addFooter(self, **kwargs):"""添加页脚:return:"""# document 对象document = self.documenttext = kwargs.get("text", None)image = kwargs.get("image", None)# section.top_margin:上页边距## section.bottom_margin:下页边距## section.left_margin:左页边距## section.right_margin:右页边距section = document.sections[0]# section.bottom_margin = Cm(5)footer = section.footerif text is not None:paragraph = footer.paragraphs[0]run = paragraph.add_run()run.add_text(text)if image is not None:paragraph = footer.paragraphs[0]run = paragraph.add_run()run.add_picture(image, width=Inches(2))# 右对齐paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT		def getWordAddWatermark(self, file, content=""):self.document.save(self.file)wordApp = win32com.client.DispatchEx("Word.Application")  # 打开word进程wordApp.Visible = TruewordApp.DisplayAlerts = False# 打开文件wordApp.Documents.Open(os.path.abspath(file))activeDoc = wordApp.ActiveDocumentwordApp.ActiveWindow.ActivePane.View.SeekView = 1for page in range(wordApp.ActiveWindow.Panes(1).Pages.Count):width = 80 * len(content)wordApp.Selection.HeaderFooter.Shapes.AddTextEffect(0, content, "等线", 1, False, False, 0, 0).Select()wordApp.Selection.ShapeRange.Line.Visible = False# 透明度wordApp.Selection.ShapeRange.Fill.Transparency = 0.2# 设置前景色wordApp.Selection.ShapeRange.Fill.ForeColor = 0wordApp.Selection.ShapeRange.Fill.ForeColor.RGB = 12632256wordApp.Selection.ShapeRange.Fill.ForeColor.TintAndShade = 0# 设置方向wordApp.Selection.ShapeRange.Rotation = 350wordApp.Selection.ShapeRange.LockAspectRatio = True# 文本# wordApp.Selection.ShapeRange.Height = 50wordApp.Selection.ShapeRange.Width = width# 边框wordApp.Selection.ShapeRange.WrapFormat.Type = 3wordApp.Selection.ShapeRange.WrapFormat.Side = 3# 位置wordApp.Selection.ShapeRange.Left = 20wordApp.Selection.ShapeRange.Top = 300# 关闭页眉页脚wordApp.ActiveWindow.ActivePane.View.SeekView = 0activeDoc.Save()activeDoc.Close()wordApp.Quit()			


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部