-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
research on PDF files merge in horizontally
- Loading branch information
Xiaoqi Zhao
committed
Dec 24, 2023
1 parent
5805d73
commit 264705f
Showing
12 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+81.1 KB
...on-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/High_Level_TQRDC.pdf
Binary file not shown.
Binary file added
BIN
+1010 KB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a-out.pdf
Binary file not shown.
Binary file added
BIN
+1.1 MB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a1.pdf
Binary file not shown.
Binary file added
BIN
+1010 KB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b-out.pdf
Binary file not shown.
Binary file added
BIN
+1.1 MB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b1.pdf
Binary file not shown.
20 changes: 20 additions & 0 deletions
20
..._Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/get_info.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from PyPDF2 import PdfReader | ||
|
||
reader = PdfReader("./a.pdf") | ||
|
||
meta = reader.metadata | ||
|
||
print(meta.author) | ||
|
||
print(meta.creator) | ||
|
||
print(meta.producer) | ||
|
||
print(meta.title) | ||
|
||
print(meta.subject) | ||
|
||
for i in range(len(reader.pages)): | ||
page = reader.pages[i].rotate(90) | ||
page = reader.pages[i] | ||
print(page.mediabox.lower_left, page.mediabox.lower_right, page.mediabox.upper_left, page.mediabox.upper_right) |
Binary file added
BIN
+3.5 MB
...2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/grabit.pdf
Binary file not shown.
Binary file added
BIN
+19.8 MB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-a-b.pdf
Binary file not shown.
18 changes: 9 additions & 9 deletions
18
...l/merge_mutil/h-merge_pypdf3_multipage.py → ...erge_mutil/h-merge_pypdf3_multipage_v2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
# Purpose: Merge two PDFs (with same total pages) in side by side horizontally | ||
# Author: Xiaoqi Zhao | ||
# Version 2: 2023/12/24, remove dependency from PageObject | ||
# Version 1: 2023/12/23, initialization | ||
|
||
from PyPDF3 import PdfFileReader, PdfFileWriter | ||
from PyPDF3.pdf import PageObject | ||
|
||
pdf_filenames = ["./a.pdf", "./b.pdf"] | ||
pdf_filenames = ["./a1.pdf", "./b1.pdf"] | ||
|
||
reader1 = PdfFileReader(open(pdf_filenames[0], "rb"), strict=False) | ||
reader2 = PdfFileReader(open(pdf_filenames[1], "rb"), strict=False) | ||
|
||
writer = PdfFileWriter() | ||
|
||
page1 = reader1.getPage(0) | ||
page2 = reader2.getPage(0) | ||
|
||
for i in range(len(reader1.pages)): | ||
|
||
page1 = reader1.getPage(i) | ||
|
||
page2 = reader2.getPage(i) | ||
|
||
total_width = page1.mediaBox.upperRight[0] + page2.mediaBox.upperRight[0] | ||
total_height = max([page1.mediaBox.upperRight[1], page2.mediaBox.upperRight[1]]) | ||
# print(total_width, total_height) | ||
print(total_width, total_height) | ||
|
||
new_page = writer.insertBlankPage(total_width, total_height, i) | ||
|
||
new_page.mergePage(page1) | ||
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0) | ||
|
||
# writer.addPage(new_page) | ||
|
||
writer.write(open("./h-merge_pypdf3-multipage.pdf", "wb")) | ||
writer.write(open("./h-a-b.pdf", "wb")) |
43 changes: 43 additions & 0 deletions
43
...ce/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Purpose: Merge two PDFs (with same total pages) in side by side horizontally | ||
# Author: Xiaoqi Zhao | ||
# Version 3: 2023/12/24, support merging of two PDFs with different total pages | ||
# Version 2: 2023/12/24, remove dependency from PageObject | ||
# Version 1: 2023/12/23, initialization | ||
|
||
from PyPDF3 import PdfFileReader, PdfFileWriter | ||
|
||
pdf_filenames = ["./grabit.pdf", "./High_Level_TQRDC.pdf"] | ||
|
||
reader1 = PdfFileReader(open(pdf_filenames[0], "rb"), strict=False) | ||
reader2 = PdfFileReader(open(pdf_filenames[1], "rb"), strict=False) | ||
|
||
writer = PdfFileWriter() | ||
|
||
for i in range(max(len(reader1.pages), len(reader2.pages))): | ||
|
||
if i < len(reader1.pages): | ||
page1 = reader1.getPage(i) | ||
|
||
if i < len(reader2.pages): | ||
page2 = reader2.getPage(i) | ||
|
||
total_width = page1.mediaBox.upperRight[0] + page2.mediaBox.upperRight[0] | ||
total_height = max([page1.mediaBox.upperRight[1], page2.mediaBox.upperRight[1]]) | ||
print(total_width, total_height) | ||
|
||
new_page = writer.insertBlankPage(total_width, total_height, i) | ||
|
||
if len(reader1.pages) > len(reader2.pages): | ||
if i > len(reader2.pages): | ||
new_page.mergePage(page1) | ||
else: | ||
new_page.mergePage(page1) | ||
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0) | ||
else: | ||
if i > len(reader1.pages): | ||
new_page.mergePage(page1) | ||
else: | ||
new_page.mergePage(page1) | ||
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0) | ||
|
||
writer.write(open("./test.pdf", "wb")) |
Binary file added
BIN
+4.34 MB
part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/test.pdf
Binary file not shown.