diff --git a/Python-with-Office-Video_Covers.pptx b/Python-with-Office-Video_Covers.pptx index d87250f..f030dbf 100644 Binary files a/Python-with-Office-Video_Covers.pptx and b/Python-with-Office-Video_Covers.pptx differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/High_Level_TQRDC.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/High_Level_TQRDC.pdf new file mode 100644 index 0000000..c5fccff Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/High_Level_TQRDC.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a-out.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a-out.pdf new file mode 100644 index 0000000..66cdc88 Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a-out.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a1.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a1.pdf new file mode 100644 index 0000000..e6db07c Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/a1.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b-out.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b-out.pdf new file mode 100644 index 0000000..66cdc88 Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b-out.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b1.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b1.pdf new file mode 100644 index 0000000..9e1399e Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/b1.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/get_info.py b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/get_info.py new file mode 100644 index 0000000..f1a71f7 --- /dev/null +++ b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/get_info.py @@ -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) \ No newline at end of file diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/grabit.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/grabit.pdf new file mode 100644 index 0000000..c40e9b1 Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/grabit.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-a-b.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-a-b.pdf new file mode 100644 index 0000000..b83a0cc Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-a-b.pdf differ diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage.py b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v2.py similarity index 66% rename from part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage.py rename to part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v2.py index 6a309bb..6b7300b 100644 --- a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage.py +++ b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v2.py @@ -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")) \ No newline at end of file +writer.write(open("./h-a-b.pdf", "wb")) \ No newline at end of file diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v3.py b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v3.py new file mode 100644 index 0000000..6d4be42 --- /dev/null +++ b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/h-merge_pypdf3_multipage_v3.py @@ -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")) \ No newline at end of file diff --git a/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/test.pdf b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/test.pdf new file mode 100644 index 0000000..e446d6c Binary files /dev/null and b/part2_Use-Python-in-Office/ch09_work-with-PDF/9-add-1_merge-horizontal/merge_mutil/test.pdf differ