##########################
#working with xlsx files #
##########################
import openpyxl
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment
import os
import datetime
my_cellstatus_headders = ['Server Name','Cell Name','Role','Current Status','Expected Status','Health']
log_path = 'C:\\Users\\686559\\Desktop\\TESTPERL\\TESTPYTHON\\Create_excel\\bin\\Infra_Health_Status.xlsx'
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin'))
alignment = Alignment(horizontal='center',vertical='bottom',text_rotation=0,wrap_text=False,shrink_to_fit=False,indent=0)
try:
mywb = openpyxl.Workbook()
except:
print ("Couldn't create the file ", log_path)
sheet = mywb.active
sheet.title = "Cell_Status"
for i in range (1,7):
sheet.cell(row=1,column=i).value = my_cellstatus_headders[i-1]
sheet.cell(row=1,column=i).font = Font(bold=True)
sheet.cell(row=1,column=i).fill = PatternFill(fgColor="D8D8D8", fill_type = "solid")
sheet.cell(row=1,column=i).border = thin_border
sheet.cell(row=1,column=i).alignment = alignment
columns = sheet.columns
#help (columns)
for col in columns:
max_length = 0
#print (col)
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = int(len(cell.value))
except:
pass
adjusted_width = max_length
sheet.column_dimensions['col'].width = adjusted_width
try:
mywb.save(log_path)
except:
print ("Could not save the file ",log_path)
but the excel sheet which is getting created is not getting the width of the column set to the max characters in the cell. Any help or idea is appreciated.