কম্পিউটার টিউটোরিয়াল

পাইথনে PDF ফাইল হ্যান্ডলিং: PyPDF2 লাইব্রেরির ব্যবহার

পাইথন একটি বহুমুখী ল্যাঙ্গুয়েজ কারণ এটি বিভিন্ন কাজের জন্য বিশাল লাইব্রেরি প্রদান করে। আমরা সবাই পোর্টেবল ডকুমেন্ট ফরম্যাট (PDF) ফাইলের সাথে কাজ করি। পাইথনে PDF ফাইল হ্যান্ডল করার বিভিন্ন পদ্ধতি রয়েছে। এই লেখায় PyPDF2 লাইব্রেরি ব্যবহার করে PDF ফাইল নিয়ে কাজ করা দেখানো হলো।

PyPDF2 একটি পিউর-পাইথন PDF লাইব্রেরি যা PDF ফাইলগুলোর পেজ স্প্লিট, মার্জ, ক্রপ এবং ট্রান্সফর্ম করতে সক্ষম। এছাড়া এটি কাস্টম ডেটা, ভিউইং অপশন এবং পাসওয়ার্ড যোগ করতে পারে। এটি PDF থেকে টেক্সট এবং মেটাডেটা বের করতে পারে এবং পুরো ফাইল মার্জ করতে পারে। এক句话说তে, PyPDF2 PDF-র জন্য একটি স্বিস-আর্মি নাইফ।

শুরু করা যাক

PyPDF2 একটি স্ট্যান্ডার্ড পাইথন প্যাকেজ, তাই এটিকে ইনস্টল করতে হবে। ভালো খবর হলো এটি খুব সহজে pip দিয়ে ইনস্টল করা যায়। কমান্ড টার্মিনালে নিচের কমান্ড রান করুন:

C:\Users\rajesh>pip install pypdf2
Collecting pypdf2
Downloading https://files.pythonhosted.org/packages/.../PyPDF2-1.26.0.tar.gz (77kB)
100% |████████████████████| 81kB 83kB/s
Building wheels for collected packages: pypdf2
Successfully built pypdf2
Installing collected packages: pypdf2
Successfully installed pypdf2-1.26.0

ভেরিফাই করতে পাইথন শেল থেকে ইম্পোর্ট করুন:

>>> import PyPDF2
>>> Successful, Great.

মেটাডেটা বের করা (Extracting Metadata)

যেকোনো PDF থেকে গুরুত্বপূর্ণ ইনফরমেশন বের করা যায়: অথর, টাইটেল, সাবজেক্ট, পেজ সংখ্যা ইত্যাদি।

from PyPDF2 import PdfFileReader

def extract_pdfMeta(path):
    with open(path, 'rb') as f:
        pdf = PdfFileReader(f)
        info = pdf.getDocumentInfo()
        number_of_pages = pdf.getNumPages()
    
    print("Author: \t", info.author)
    print("Creator: \t", info.creator)
    print("Producer: \t", info.producer)
    print("Subject: \t", info.subject)
    print("Title: \t", info.title)
    print("Number of Pages in pdf: \t", number_of_pages)

if __name__ == '__main__':
    path = 'DeepLearning.pdf'
    extract_pdfMeta(path)

আউটপুট

Author: Nikhil Buduma,Nicholas Locascio
Creator: AH CSS Formatter V6.2 MR4 for Linux64 : 6.2.6.18551 (2014/09/24 15:00JST)
Producer: Antenna House PDF Output Library 6.2.609 (Linux64)
Subject: None
Title: Fundamentals of Deep Learning
Number of Pages in pdf: 298

PDF ফাইল খোলা ছাড়াই কিছু উপকারী তথ্য পাওয়া গেল।

PDF থেকে টেক্সট বের করা (Extracting Text from PDFs)

PDF থেকে টেক্সট বের করা যায়, যদিও ইমেজ বের করার বিল্ট-ইন সাপোর্ট সীমিত।

উদাহরণস্বরূপ, ৫০ নম্বর পেজ থেকে টেক্সট বের করা:

from PyPDF2 import PdfFileReader

def extract_pdfText(path):
    with open(path, 'rb') as f:
        pdf = PdfFileReader(f)
        page = pdf.getPage(50)
        print(page)
        print('Page type: {}'.format(str(type(page))))
        text = page.extractText()
        print(text)

if __name__ == '__main__':
    path = 'DeepLearning.pdf'
    extract_pdfText(path)

আউটপুট

{'/Annots': IndirectObject(1421, 0), '/Contents': IndirectObject(179, 0), ...}
Page type: 
time. In inverted dropout, any neuron whose activation hasn†t been silenced has its output divided by p before the value is propagated to the next layer. With this fix, Eoutput=p†xp+1†fnof; p†0= x, and we can avoid arbitrarily scaling neuronal output at test time. SummaryIn this chapter, we†ve learned all of the basics involved in training feed-forward neural networks. We†ve talked about gradient descent, the backpropagation algorithm, as well as various methods we can use to prevent overfitting. In the next chapter, we†ll put these lessons into practice when we use the TensorFlow library to efficiently implement our first neural networks. Then in Chapter 4 , we†ll return to the problem of optimizing objective functions for training neural networks and design algorithmsto significantly improve performance. These improvements will enable us to process much more data, which means we†ll be able to build more comprehensive models. Summary | 37

টেক্সট পাওয়া গেলেও এটি পরিষ্কার নয়। দুর্ভাগ্যবশত, PyPDF2-এর টেক্সট এক্সট্রাকশন সাপোর্ট সীমিত।

PDF পেজ রোটেট করা

>>> import PyPDF2
>>> deeplearningFile = open('DeepLearning.pdf', 'rb')
>>> pdfReader = PyPDF2.PdfFileReader(deeplearningFile)
>>> page = pdfReader.getPage(0)
>>> page.rotateClockwise(90)
{ '/Contents': [...], '/CropBox': [0, 0, 595.3, 841.9], '/MediaBox': [0, 0, 504, 661.5], '/Parent': IndirectObject(4862, 0), '/Resources': IndirectObject(4889, 0), '/Rotate': 90, '/Type': '/Page' }
>>> pdfWriter = PyPDF2.PdfFileWriter()
>>> pdfWriter.addPage(page)
>>> resultPdfFile = open('rotatedPage.pdf', 'wb')
>>> pdfWriter.write(resultPdfFile)
>>> resultPdfFile.close()
>>> deeplearningFile.close()

আউটপুট

(রোটেট করা PDF পেজের ছবি)

  1. পাইথনে PDF ফাইল এক্সেল ফাইলে কনভার্ট করা

  2. পাইথনে PDF ফাইল ক্র্যাক (পাসওয়ার্ড খোলা) করা

  3. পাইথনের অভ্যন্তরীণ কাজকর্ম

  4. পাইথনে ইমেজ প্রসেসিং (Working with Images in Python)