Question: Hey Ahmad, I have wrote this python code to convert pdf file to . COB I am not receiving the output files, that is I

Hey Ahmad,
I have wrote this python code to convert pdf file to .COB I am not receiving the output files, that is I am not getting the .COB can you assist?
# importing required classes
from PyPDF2 import PdfReader
import os
from openbabel import openbabel as ob
def pdf_to_text(pdf_file):
text =""
# Open the PDF file
with open(pdf_file, 'rb') as file:
reader = PdfReader(file)
# Extract text from each page
for page_num in range(len(reader.pages)):
page = reader.pages[page_num] # Fixed from page_number to page_num
text += page.extract_text()
return text
def text_to_cob(text, cob_file):
# Initialize OpenBabel converter
conv = ob.OBConversion()
conv.SetInAndOutFormats("smi", "cob")
# Convert text to COB format
mol = ob.OBMol()
conv.ReadString(mol, text)
# Print the converted text
converted_text = conv.WriteString(mol)
print("Converted text to COB format:")
print(converted_text)
# Write COB file
conv.WriteFile(mol, cob_file)
print(f"COB file '{cob_file}' created successfully.")
def batch_convert_pdf_to_cob(pdf_folder, cob_folder):
# Iterate through all PDF files in the folder
for filename in os.listdir(pdf_folder):
if filename.endswith('.pdf'):
pdf_file = os.path.join(pdf_folder, filename)
print(f"Processing PDF file: {pdf_file}")
# Open the PDF file
with open(pdf_file, 'rb') as file:
pdf_reader = PdfReader(file)
# Process the PDF content
# For example, you can access pages using pdf_reader.pages
# and extract text using page.extract_text() method
# You need to define your logic here based on what you want to do with the PDF content
tex pdfto_text(pdf_file)
cob_file = os.path.join(cob_folder, os.path.splitext(filename)[0]+'.cob')
text_to_cob(text, cob_file)
print(f"COB file '{cob_file}' created successfully.")
# Specify input and output folders
pdf_folder = r'C:\Users\c1238\Documents\mainframe_COBOL'
cob_folder = r'C:\Users\c1238\Documents\output_cob_folder'
# Perform batch conversion
batch_convert_pdf_to_cob(pdf_folder,cob_folder)
this is the output: Processing PDF file: C:\Users\c1238\Documents\mainframe_COBOL\Z990010L.pdf
Converted text to COB format:
COB file 'C:\Users\c1238\Documents\output_cob_folder\Z990010L.cob' created successfully.
COB file 'C:\Users\c1238\Documents\output_cob_folder\Z990010L.cob' created successfully.
Processing PDF file: C:\Users\c1238\Documents\mainframe_COBOL\Z990010P.pdf
Converted text to COB format:
what's up??? get the correct message but not conversion files

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!