[PYTHON] Convert A4 PDF to A3 every 2 pages

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

Convert A4 PDF to A3 every 2 pages
Convert PDF to Documents by OCR
Convert markdown to PDF in Python
Convert from pdf to txt 2 [pyocr]
Convert a string to an image
Convert PDF to image with ImageMagick
A tool to convert Juniper config
Convert a large number of PDF files to text files using pdfminer
[Python] How to convert a 2D list to a 1D list
Convert from PDF to CSV with pdfplumber
Convert PDF attached to email to text format
Convert PDF files to PNG files with GIMP
Renumber PDF pages
I made a CLI tool to convert images in each directory to PDF
I want to convert a table converted to PDF in Python back to CSV
Convert PDF to image (JPEG / PNG) with Python
How to convert SVG to PDF and PNG [Python]
Convert a multidimensional list (array) to one dimension
Convert multiple jpg files to one PDF file
Batch convert PSD files in directory to PDF
Write a script to convert a MySQL dump to TSV
[Small story] Easy way to convert Jupyter to PDF
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
How to convert / restore a string with [] in python
Beginners try to convert Word files to PDF at once
Convert a slice object to a list of index numbers
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert a text file with hexadecimal values to a binary file
How to convert a class object to a dictionary with SQLAlchemy
Convert jupyter to py
[Python] Convert PDF text to CSV page by page (2/24 postscript)
Convert keras-yolo3 to onnx
Convert the image in .zip to PDF with Python
I made a code to convert illustration2vec to keras model
Convert dict to array
How to convert a mel spectrogram back to a wav file
[Python] Created a method to convert radix in 1 second
Convert json to excel
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to convert an array to a dictionary with Python [Application]
How to output a document in pdf format with Sphinx
Probably the easiest way to create a pdf with Python3
Experiment to make a self-catering PDF for Kindle with Python
Batch convert image files uploaded to MS Forms / Google Forms to PDF
Convert garbled scanned images to PDF with Pillow and PyPDF
Convert the image data (png) at hand to a .pbm image
A quick introduction to pytest-mock
Convert hexadecimal string to binary
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
A road to intermediate Python
A super introduction to Linux
Convert HTML to text file
Add a dictionary to MeCab
How to call a function
Add page number to PDF
Upload a file to Dropbox
Send a signal to subprocess
How to hack a terminal