import argparse
import openpyxl
import ibm_db_dbi
from copy import copy
def copy_cell_formatting(source_cell, target_cell):
"""
Copies all formatting from source_cell to target_cell.
"""
target_cell.font = copy(source_cell.font)
target_cell.border = copy(source_cell.border)
target_cell.fill = copy(source_cell.fill)
target_cell.number_format = copy(source_cell.number_format)
target_cell.protection = copy(source_cell.protection)
target_cell.alignment = copy(source_cell.alignment)
def generate_report(template_path, output_path, data_start="A1"):
# Load the workbook and select the active worksheet
wb = openpyxl.load_workbook(template_path)
ws = wb.active
# Connect to the database
conn = ibm_db_dbi.connect()
cursor = conn.cursor()
# Execute the query to fetch employee data
sql = "SELECT EMPNO, FIRSTNME, LASTNAME, JOB, SALARY FROM SAMPLE.EMPLOYEE"
cursor.execute(sql)
# Parse the data_start to get row and column correctly for any column
column, row = data_start[0], data_start[1:]
start_row = int(row)
start_col = openpyxl.utils.column_index_from_string(column)
# Insert data into the worksheet starting from data_start
for row_index, db_row in enumerate(cursor, start=start_row):
for col_index, value in enumerate(db_row, start=start_col):
# The template cell for formatting is in the first row (data_start row)
template_cell = ws.cell(row=start_row, column=col_index)
target_cell = ws.cell(row=row_index, column=col_index, value=value)
# Copy formatting from the template cell to the new cell
copy_cell_formatting(template_cell, target_cell)
# Close the database cursor and connection
cursor.close()
conn.close()
# Save the modified workbook to the specified output path
wb.save(output_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate an Excel report from DB2 data.")
parser.add_argument("template_path", help="Path to the Excel template file.")
parser.add_argument("output_path", help="Path where the output Excel file will be saved.")
parser.add_argument("--data-start", default="A1", help="Cell position where data insertion begins (e.g., 'A6').")
args = parser.parse_args()
generate_report(args.template_path, args.output_path, args.data_start)