title: Capture part of pdf file in python
body: <p>I have pdf file of two pages and there is a barcode at the top of the first page like that</p>
  <p><a href="https://i.stack.imgur.com/WW0N3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WW0N3.png" alt="enter image description here" /></a></p>
  <p>Is there a way to capture the barcode only from such scanned pdf file in python? I have searched but didn't find such issue. Can you help me with even any link as a clue and I will study the link and try to implement it by myself?</p>
  <p>I find such a code but couldn't modify to crop the top part of the first page only</p>
  <pre><code>from PyPDF2 import PdfFileWriter, PdfFileReader
  
  with open(&quot;Sample.pdf&quot;, &quot;rb&quot;) as in_f:
      input1 = PdfFileReader(in_f)
      output = PdfFileWriter()
      
      numPages = input1.getNumPages()
      print(&quot;Document Has %s pages.&quot; % numPages)
      
      for i in range(numPages):
          page = input1.getPage(i)
          print(page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y())
          page.trimBox.lowerLeft = (25, 25)
          page.trimBox.upperRight = (225, 225)
          page.cropBox.lowerLeft = (50, 50)
          page.cropBox.upperRight = (200, 200)
          output.addPage(page)
  
      with open(&quot;Output.pdf&quot;, &quot;wb&quot;) as out_f:
          output.write(out_f)
  </code></pre>