If you have a desire to assign an A4 PDF to the left and right every two pages to make it A3,
"A4 PDF is attached to A3 on two sides (2in1) --Python (PyPDF2)" https://qiita.com/miko/items/054b982700c6219c7fce
I found it, so I'll arrange it a little. In other words, for example, if you have a PDF of 10 pages of A4, you can convert it to a PDF of 5 pages of A3 with 2in1. And if the total number of pages is odd, the right side of the last page will be blank and all pages will be converted to A3 anyway.
pdf_A3.py
import PyPDF2
A3_width = 1190.5511811024
A3_height = 841.8897637795
#Reading the original A4 file
pdf_file = open('***.pdf','rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
#Understanding the number of pages in the original A4 file
page_num = pdf_reader.getNumPages()
cnt = 1
for start_page in range(0, page_num, 2):
A3_page = PyPDF2.pdf.PageObject.createBlankPage(width=A3_width, height=A3_height)
end_page = start_page + 1
#Place odd pages on the left
page_left = pdf_reader.getPage(start_page)
A3_page.mergePage(page_left)
#Place even pages on the right, but leave the right side of the last A3 page blank when the total number of pages is odd
if start_page + 1 < page_num:
page_right = pdf_reader.getPage(end_page)
A3_page.mergeRotatedScaledTranslatedPage(page_right, 0, 1, A3_width / 2, 0, expand=False)
else:
pass
pdf_writer = PyPDF2.PdfFileWriter()
pdf_writer.addPage(A3_page)
#A magic spell to add a zero-padded 4-digit number after the generated file name
file_num = str(cnt).zfill(4)
file_name = 'test_A3_' + file_num + '.pdf'
output_file = open(file_name,'wb')
pdf_writer.write(output_file)
output_file.close()
cnt += 1
pdf_file.close()
print ('End!')
It's a program that I don't know what the demand is, but since the screen of the personal computer is horizontally long, I would like to insist that it would be easier to see if the PDF of A4 is displayed side by side in A3, but the display is set with the viewer setting like Acrobat Reader. It's just a matter of making it a "spread page".
Recommended Posts