-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpdf-reader-6.py
More file actions
34 lines (24 loc) · 1.48 KB
/
pdf-reader-6.py
File metadata and controls
34 lines (24 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Добавление водяного знака в одностраничный PDF
import PyPDF2
input_file = "source/Computer-Vision-Resources.pdf"
output_file = "dist/Computer-Vision-Resources-page-drafted.pdf"
watermark_file = "source/mshe-logo-512x512.pdf"
with open(input_file, "rb") as filehandle_input:
# читать содержимое исходного файла
pdf = PyPDF2.PdfFileReader(filehandle_input)
with open(watermark_file, "rb") as filehandle_watermark:
# читать содержание водяного знака
watermark = PyPDF2.PdfFileReader(filehandle_watermark)
# получить первую страницу оригинального PDF
first_page = pdf.getPage(0)
# получить первую страницу водяного знака PDF
first_page_watermark = watermark.getPage(0)
# объединить две страницы
first_page.mergePage(first_page_watermark)
# создать объект записи PDF для выходного файла
pdf_writer = PyPDF2.PdfFileWriter()
# добавить страницу
pdf_writer.addPage(first_page)
with open(output_file, "wb") as filehandle_output:
# записать файл с водяными знаками в новый файл
pdf_writer.write(filehandle_output)