Yes, I can see the confusing. I'm thinking in Excel and not coding terms
Let's say I have a SS that is 4 columns across by 40 rows down. It fits on one A4 page when printed. I've got thin borders on every cell on all sides.
What I want to add is a thick border around the outside cells of the printed page.
So in column 1(A), each cell would have a thick border on the left side, and a thin borders on the top, bottom, and right sides.
In column 4(D), each cell would have a thick border on the right side, and a thin borders on the top, bottom, and left sides.
In the top row (1), the top 4 cells (columns 1 to 4) would have a thick border on the top side, and a thin borders on the bottom, left and right sides, with the exception of the two corner cells (columns 1 & 4) which have also have the outside cells thick as noted above.
And the bottom row is opposite of the top row.
I suppose now that Ive written this out like this, I will try to hand code it myself. I'm wondering if there's any feature in openpyxl to do this or advice on the most efficient way in Python to do this.
Thank you,
Phil
#! python'''Border options:'dashed', 'dotted', 'double', 'hair', 'thin', 'thick', 'mediumDashDot', 'slantDashDot', 'medium', 'mediumDashDotDot', 'dashDot', 'dashDotDot', 'mediumDashed''''# Importsimport os # import openpyxl # from openpyxl.styles import Border, Side# Alignment
print('cwd = ', os.getcwd())# os.chdir ('c:\\users\pc\desktop') # CHANGE DIRECTORY FOR FILE LOCATION
wb = openpyxl.Workbook()print(' Created a new Excel Spreadsheet')print ("New spreadsheet type = ", type (wb)) # print('all sheetnames = ', wb.sheetnames)sheet = wb.activeprint('sheet = ', sheet)
borderThick = Border(left=Side(style='thick'), right=Side(style='thick'), top=Side(style='thick'), bottom=Side(style='thick')) #
borderThin = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin')) #
borderTop = Border(top=Side(style='thick'), left=Side(style='thin'), right=Side(style='thin'), bottom=Side(style='thin'))
borderBottom = Border(bottom=Side(style='thick'), left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
borderLeft = Border(left=Side(style='thick'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin')) # borderRight = Border(right=Side(style='thick'), left=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin')) #
borderTopLeft = Border(left=Side(style='thick'), right=Side(style='thin'), top=Side(style='thick'), bottom=Side(style='thin')) #
borderTopRight = Border(left=Side(style='thin'), right=Side(style='thick'), top=Side(style='thick'), bottom=Side(style='thin')) #
borderBottomLeft = Border(left=Side(style='thick'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thick')) #
borderBottomRight = Border(left=Side(style='thin'), right=Side(style='thick'), top=Side(style='thin'), bottom=Side(style='thick')) #
borderSide = Side(border_style='thick') # applies to sides of each cell
# Set thin borders on entire sheetrows = range(1, 46)rowTop = int(1)rowBot = int(45)colLeft = int(1)colRight = int(10)columns = range(1, 11)for row in rows: for col in columns: sheet.cell(row, col).border = borderThin
# Set thick outer bordersfor row in rows: for col in columns: sheet.cell(rowTop, col).border = borderTop sheet.cell(rowBot, col).border = borderBottom sheet.cell(row, colLeft).border = borderLeft sheet.cell(row, colRight).border = borderRight sheet['A1'].border = borderTopLeft sheet['J1'].border = borderTopRight sheet['A45'].border = borderBottomLeft sheet['J45'].border = borderBottomRight
# Saving an Excel Spreadsheetprint(' save the empty file')wb.save('openpyxl_styles_cellBorders.xlsx') #