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

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

import json
import os

import win32com
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt, Inches, Cm
from win32com.client import Dispatch

class 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.document
        text = kwargs.get("text", None)
        image = kwargs.get("image", None)

        section = document.sections[0]
        header = section.header
        if 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.document
        text = 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.footer
        if 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 = True
        wordApp.DisplayAlerts = False

        # 打开文件
        wordApp.Documents.Open(os.path.abspath(file))

        activeDoc = wordApp.ActiveDocument
        wordApp.ActiveWindow.ActivePane.View.SeekView = 1
        for 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 = 0
            wordApp.Selection.ShapeRange.Fill.ForeColor.RGB = 12632256
            wordApp.Selection.ShapeRange.Fill.ForeColor.TintAndShade = 0

            # 设置方向
            wordApp.Selection.ShapeRange.Rotation = 350
            wordApp.Selection.ShapeRange.LockAspectRatio = True
            # 文本
            # wordApp.Selection.ShapeRange.Height = 50
            wordApp.Selection.ShapeRange.Width = width

            # 边框
            wordApp.Selection.ShapeRange.WrapFormat.Type = 3
            wordApp.Selection.ShapeRange.WrapFormat.Side = 3
            # 位置
            wordApp.Selection.ShapeRange.Left = 20
            wordApp.Selection.ShapeRange.Top = 300

        # 关闭页眉页脚
        wordApp.ActiveWindow.ActivePane.View.SeekView = 0
        activeDoc.Save()
        activeDoc.Close()
        wordApp.Quit()			
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要去除PDF文件中的页眉页脚,可以使用Python中的第三方库PyPDF2来实现。 首先,我们需要安装PyPDF2库。可以使用以下命令在命令行中安装库: ``` pip install PyPDF2 ``` 安装完成后,我们可以编写Python代码来去除PDF文件的页眉页脚。具体代码如下: ```python import PyPDF2 # 打开PDF文件 pdf_file = open('example.pdf', 'rb') # 创建PDF阅读器对象 pdf_reader = PyPDF2.PdfReader(pdf_file) # 创建PDF写入器对象 pdf_writer = PyPDF2.PdfWriter() # 遍历PDF页面 for page_num in range(pdf_reader.numPages): # 获取当前页面 page = pdf_reader.getPage(page_num) # 从页面中提取内容框 content_box = page.mediaBox # 更新内容框的上边界和下边界 content_box.upperRight = (content_box.upperRight[0], content_box.upperRight[1]-50) # 更新上边界 content_box.lowerLeft = (content_box.lowerLeft[0], content_box.lowerLeft[1]+50) # 更新下边界 # 创建新页面,并将内容框更新后的页面添加到PDF写入器中 new_page = pdf_writer.add_blank_page(width=page.mediaBox.getWidth(), height=page.mediaBox.getHeight()) # 创建新页面 new_page.mergeTranslatedPage(page, tx=0, ty=-50) # 添加内容框更新后的页面 # 将处理后的 PDF 页面写入新文件 output_file = open('new_example.pdf', 'wb') pdf_writer.write(output_file) # 关闭文件 pdf_file.close() output_file.close() ``` 以上代码将打开`example.pdf`文件,并遍历每个页面,通过更新内容框的上边界和下边界来去除页眉页脚。然后将处理后的页面写入一个新的PDF文件`new_example.pdf`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值