0% found this document useful (0 votes)
4 views

Automating The Upload of Excel Files From Oracle Application Framework

This document provides a guide to automating the upload of Excel files from Oracle Application Framework to another system using Python. It discusses connecting to an Oracle database, extracting data, saving it to an Excel file, and automating the process on a schedule. An example Python script is provided with explanations of the key steps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Automating The Upload of Excel Files From Oracle Application Framework

This document provides a guide to automating the upload of Excel files from Oracle Application Framework to another system using Python. It discusses connecting to an Oracle database, extracting data, saving it to an Excel file, and automating the process on a schedule. An example Python script is provided with explanations of the key steps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Automating the upload of Excel files from Oracle Application Framework (OAF) to another

system or database can be complex, but it generally involves several key steps. Here's a
comprehensive guide on how you can achieve this using Python and relevant libraries such as
cx_Oracle for database interaction, pandas for handling Excel files, and openpyxl for
working with Excel files.

Prerequisites

1. Oracle Database Connection:


o Install the cx_Oracle library to interact with Oracle databases.
o Ensure you have the Oracle Instant Client installed and properly configured.
2. Excel File Handling:
o Install pandas and openpyxl libraries to read and write Excel files.
3. Automation and Scheduling:
o Use a scheduling library like schedule or cron jobs for automation.

Installation of Required Libraries

Install the necessary Python libraries using pip:

sh
pip install cx_Oracle pandas openpyxl schedule

Step-by-Step Guide

1. Connect to the Oracle Database:


o Use cx_Oracle to establish a connection to the Oracle database.
2. Download Data from OAF:
o Extract data from OAF using SQL queries.
3. Save Data to Excel:
o Use pandas to create and save an Excel file.
4. Upload Excel File:
o Upload the Excel file to the desired location, which could be an FTP server, a
local directory, or a web application.
5. Automate the Process:
o Use a scheduling tool to automate the entire process.

Example Script

Here’s an example script that demonstrates the steps mentioned above:

python
import cx_Oracle
import pandas as pd
import schedule
import time
import os

# Database connection configuration


dsn_tns = cx_Oracle.makedsn('HOST', 'PORT', service_name='SERVICE_NAME')
connection = cx_Oracle.connect(user='USERNAME', password='PASSWORD',
dsn=dsn_tns)

def fetch_data_from_oaf():
try:
cursor = connection.cursor()
sql_query = "SELECT * FROM YOUR_TABLE"
cursor.execute(sql_query)
columns = [col[0] for col in cursor.description]
data = cursor.fetchall()
cursor.close()

# Convert the data to a pandas DataFrame


df = pd.DataFrame(data, columns=columns)

# Save the DataFrame to an Excel file


excel_file_path = 'data_from_oaf.xlsx'
df.to_excel(excel_file_path, index=False)

print(f"Data successfully saved to {excel_file_path}")

# Optional: Upload the file to a server or cloud storage


# upload_to_ftp(excel_file_path)
# upload_to_s3(excel_file_path)

except cx_Oracle.DatabaseError as e:
print(f"Error fetching data from OAF: {e}")

def upload_to_ftp(file_path):
# Implement FTP upload logic here
pass

def upload_to_s3(file_path):
# Implement S3 upload logic here
pass

# Schedule the task to run daily at a specific time


schedule.every().day.at("10:00").do(fetch_data_from_oaf)

print("Scheduler started...")
while True:
schedule.run_pending()
time.sleep(1)

Explanation

1. Database Connection:

python
1. dsn_tns = cx_Oracle.makedsn('HOST', 'PORT',
service_name='SERVICE_NAME')
2. connection = cx_Oracle.connect(user='USERNAME', password='PASSWORD',
dsn=dsn_tns)
3.
4. Fetch Data from OAF:
o Execute a SQL query to retrieve data from the Oracle database.
o Convert the retrieved data into a pandas DataFrame.
5. Save Data to Excel:
o Save the DataFrame to an Excel file using df.to_excel().
6. Automation:
o Use the schedule library to run the fetch_data_from_oaf function daily at a
specified time.
7. Additional Upload Logic:
o Placeholder functions upload_to_ftp and upload_to_s3 are provided for
extending the script to upload files to FTP or S3, respectively.

Running the Script

Run the script using a Python interpreter:

sh
python automate_excel_upload.py

This script will automatically fetch data from OAF, save it to an Excel file, and can be
extended to upload the file to a remote server or cloud storage. The scheduling part ensures
that this process runs at the specified time daily. Adjust the schedule and other parameters as
needed for your specific use case.

You might also like