In the process of a loop that Reads each workbook (I have like 4 separate excel files), and copy each column, cell by cell, to the new spreadsheet, Could not get the formatting for the title which was merged cells and centered AND the line/cell borders
#text formatting DATA
###############################Fomrat, Merge/Center/Bold Title, ceneter cells #########################
wb = openpyxl.load_workbook(excelAutoNamed)
ws = wb['Validation'] #wb.active can also be used here
#auto size the cells
for idx, col in enumerate(ws.columns, 1):
ws.column_dimensions[get_column_letter(idx)].auto_size = True
# Merge the top cells for the title
# Make the fill red, and text white, bolt, size 16, and centred
ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=len(df.columns)+1)
cell = ws.cell(row=1, column=1)
cell.value = award
cell.fill = PatternFill("solid", fgColor="0091ea")
cell.font = Font(b=True, size=16, color="ffffff")
cell.alignment = Alignment(horizontal="center", vertical="center")
# Centre all the cells excluding the title, column headers, and row indexes
for row in ws.iter_rows(min_row=3, min_col=2):
for cell in row:
cell.alignment = Alignment(horizontal="center", vertical="center")
I was missing two things in the combined Workbook,
NUMBER 1 - the title which was originally merged, centered, and over all of the columns corresponding to the columns of the excel file IS NO longer merged, centered and over the columns
NUMBER 2 - the border lines are also no longer visible
NUMBER 3 - the headers are not auto.width
Here is code for the Bolded Borders
#borders every cell
hardworkbook = writer.book
worksheet = writer.sheets['Validation']
border_fmt = hardworkbook.add_format({'bottom':2, 'top':2, 'left':2, 'right':2})
worksheet.conditional_format(xlsxwriter.utility.xl_range(0, 0, (len(df))+2, len(df.columns)), {'type': 'no_errors', 'format': border_fmt})