Question: # This Python 3 . xx script works fine. I need assist modifying it so that it shows the successful completion and that the file

# This Python3.xx script works fine. I need assist modifying it so that it shows the successful completion and that the file transfer to the output folder "output_cob_folder" successful and comments on the python code
# Import necessary libraries
import os
#from PyPDF4 import PdfReader
import PyPDF4
from openbabel import openbabel as ob
import os
def text_to_cob(text,cob_file):
with open(cob_file, "w") as output_file:
output_file.write(text)
def extract_text_from_pdf(pdf_file):
pdf_text =""
with open(pdf_file, "rb") as file:
pdf_reader = PyPDF4.PdfFileReader(file)
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
pdf_text += page.extractText()
return pdf_text
def generate_cobol_code(pdf_text):
cobol_code =""
lines = pdf_text.split("
")
for line in lines:
if line.strip(): # Check if the line is not empty
cobol_code += f"MOVE '{line.strip()}' TO variable_name.
"
return cobol_code
def batch_convert_pdf_to_cob(pdf_folder, cob_folder):
"""Processes all PDF files in a folder to convert them to .cob files in another folder."""
# Check and create the output folder if it doesn't exist
if not os.path.exists(cob_folder):
os.makedirs(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}")
# Extract text from PDF
pdf_text = extract_text_from_pdf(pdf_file)
if pdf_text:
cob_file = os.path.join(cob_folder, os.path.splitext(filename)[0]+'.cob')
cobol_code = generate_cobol_code(pdf_text)
text_to_cob(cobol_code, cob_file)
else:
print(f"Failed to extract text from {pdf_file}")
# 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)

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!