<jupyter_start><jupyter_text>Cropping PDFs<jupyter_code>from PyPDF2 import PdfFileReader, PdfFileWriter
# storing nb names:
# 1.0-jo-extract-prostate-data
# 2.0-jo-data-exploration
# 3.0-jo-building-predictive-model
# Rscript_knitr

direc = '../docs/'
name = '3.0-jo-building-predictive-model'
path = direc + name + '.pdf'
reader = PdfFileReader(path, 'r')
# create reader object
reader.getNumPages()
# grab the 1st page
page1 = reader.getPage(0)
# dimensions of 1st page in document
ll1 = page1.cropBox.getLowerLeft()
ul1 = page1.cropBox.getUpperLeft()
ur1 = page1.cropBox.getUpperRight()
lr1 = page1.cropBox.getLowerRight()
print('Lower Left: {ll}\nUpper Left: {ul}\nUpper Right: {ur}\nLower Right: {lr}'.format(ll=ll1, ul=ul1, ur=ur1, lr=lr1))
# capture 1st page dimensions in inches
height_in = ur1[1] / 72 
width_in = ur1[0] /72
print('Height: {h}"'.format(h=height_in))
print('Width: {w}"'.format(w=width_in))
# create the write object
writer = PdfFileWriter()
# iterate over all pages and crop
# pages are added to the writer object
for i in range(reader.getNumPages()):
    page = reader.getPage(i)

    ll = page.cropBox.getLowerLeft()
    ur = page.cropBox.getUpperRight()

    # nb 1.0 use +72 y lower bound
    # nb 2.0 use +12 (i think) y lower bound
    # nb 3.0 use +50 y lower bound
    # Rscript_knitr use + 50 y lower bound
    
    page.cropBox.setLowerLeft((0, ll[1] + 50))
    page.cropBox.setUpperRight(ur)
    writer.addPage(page)
# save the new pdf
out_name = name + '_cropped'
out_path = direc + out_name + '.pdf'

outstream = open(out_path, 'wb')
writer.write(outstream)
outstream.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13929440389294404, 'fraction_numerical': 0.031630170316301706, 'mean_word_length': 4.358306188925082, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '101206', 'license': 'MIT', 'n_tokens_mistral': 651, 'n_tokens_neox': 629, 'n_words': 155, 'path': 'notebooks/pdf_crop.ipynb', 'repo_name': 'josiwala/prostate-cancer', 'size': '1644'}