Created
December 3, 2019 12:12
-
-
Save polonez/1fc01988935607b57f6ddcd7753acc7a to your computer and use it in GitHub Desktop.
pdf add margin using pypdf2
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
from PyPDF2 import PdfFileReader, PdfFileWriter | |
from PyPDF2.pdf import PageObject | |
from tqdm import tqdm | |
def get_info(path): | |
with open(path, 'rb') as f: | |
p = PdfFileReader(f) | |
info = p.getDocumentInfo() | |
number_of_pages = p.getNumPages() | |
writer = PdfFileWriter() | |
margin = 30 | |
print(f'margin: {margin}') | |
for i in tqdm(range(number_of_pages)): | |
page = p.getPage(i) | |
new_page = writer.addBlankPage( | |
page.mediaBox.getWidth() + 2 * margin, | |
page.mediaBox.getHeight() + 2 * margin | |
) | |
new_page.mergeScaledTranslatedPage(page, 1, margin, margin) | |
# writer.addPage(new_page) | |
with open('output.pdf', 'wb') as f: | |
writer.write(f) | |
if __name__ == '__main__': | |
path = 't.pdf' | |
get_info(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment