目录
reference: How to Work With a PDF in Python
reference: 给PDF添加水印
本文使用的PDF处理库为pypdf2
Read info
def extract_information(pdf_path):
with open(pdf_path, 'rb') as f:
pdf = PdfFileReader(f)
information = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
# multiply 0.352 to convert inches to millimeters
height = float(pdf.getPage(0).mediaBox.getHeight()) * 0.352
width = float(pdf.getPage(0).mediaBox.getWidth()) * 0.352
txt = f"""
Information about {pdf_path}:
Author: {information.author}
Creator: {information.creator}
Producer: {information.producer}
Subject: {information.subject}
Title: {information.title}
Number of pages: {number_of_pages}
Height: {height:.2f}
Width: {width:.2f}
"""
print(txt)
Rotate Page
def rotate_pages(pdf_path):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(pdf_path)
# Rotate page 90 degrees to the right
page_1 = pdf_reader.getPage(0).rotateClockwise(90)
pdf_writer.addPage(page_1)
# Rotate page 90 degrees to the left
page_2 = pdf_reader.getPage(1).rotateCounterClockwise(90)
pdf_writer.addPage(page_2)
# Add a page in normal orientation
pdf_writer.addPage(pdf_reader.getPage(2))
with open('rotate_pages.pdf', 'wb') as fh:
pdf_writer.write(fh)
Merge PDFs
def merge_pdfs(paths : list, output : str):
pdf_writer = PdfFileWriter()
for path in paths:
pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
# Add each page to the writer object
pdf_writer.addPage(pdf_reader.getPage(page))
# Write out the merged PDF
with open(output, 'wb') as out:
pdf_writer.write(out)
Split PDFs
def split(path, name_of_split):
pdf = PdfFileReader(path)
for page in range(pdf.getNumPages()):
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf.getPage(page))
output = f'{name_of_split}{page}.pdf'
with open(output, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
Encrypt a PDF
def add_encryption(input_pdf, output_pdf, password):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(input_pdf)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
# or use pdf_writer.appendPagesFromReader(pdf_reader)
pdf_writer.encrypt(user_pwd=password, owner_pwd=None,
use_128bit=True)
with open(output_pdf, 'wb') as fh:
pdf_writer.write(fh)
Decrypt a PDF
def decrypt_pdf(input_pdf, output_pdf, password):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(input_pdf)
if pdf_reader.isEncrypted:
try:
pdf_reader.decrypt(password)
except:
print("Wrong password")
else:
print("File is not encrypted")
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(output_pdf, 'wb') as fh:
pdf_writer.write(fh)
Add watermark
使用word制作水印页面。
设计->水印->自定义水印,导出PDF作为watermark.pdf
。
def create_watermark(input_pdf, output, watermark):
watermark_obj = PdfFileReader(watermark)
watermark_page = watermark_obj.getPage(0)
pdf_reader = PdfFileReader(input_pdf)
pdf_writer = PdfFileWriter()
# multiply 0.352 to convert inches to millimeters
print(f"watermask height: {0.352 * float(watermark_page.mediaBox.getHeight()):.2f}, \
watermask width: {0.352 * float(watermark_page.mediaBox.getWidth()):.2f}")
# Watermark all the pages
for page in range(pdf_reader.getNumPages()):
page = pdf_reader.getPage(page)
print(f"page height: {0.352 * float(page.mediaBox.getHeight()):.2f}, \
page width: {0.352 * float(page.mediaBox.getWidth()):.2f}")
page.mergePage(watermark_page)
pdf_writer.addPage(page)
with open(output, 'wb') as out:
pdf_writer.write(out)
标签:output,Python,writer,reader,watermark,处理,pdf,PDF,page
From: https://www.cnblogs.com/coco02/p/16948917.html