Automating The Upload of Excel Files From Oracle Application Framework
Automating The Upload of Excel Files From Oracle Application Framework
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
sh
pip install cx_Oracle pandas openpyxl schedule
Step-by-Step Guide
Example Script
python
import cx_Oracle
import pandas as pd
import schedule
import time
import os
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()
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
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.
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.