Advertisement
Masoko

bulk format excel files

Mar 31st, 2022 (edited)
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. import os
  2. import warnings
  3.  
  4. import openpyxl
  5. from openpyxl.utils import get_column_letter
  6.  
  7. warnings.simplefilter("ignore")
  8.  
  9.  
  10. def set_border(ws, cell_range):
  11.     thin = openpyxl.styles.Side(border_style="thin", color="757171")
  12.     for row in ws[cell_range]:
  13.         for cell in row:
  14.             cell.border = openpyxl.styles.Border(top=thin, left=thin, right=thin, bottom=thin)
  15.             cell.comment = None
  16.             # cell.alignment = Alignment(wrap_text=True)
  17.  
  18.  
  19. def set_header(ws, cell_range):
  20.     for row in ws[cell_range]:
  21.         for cell in row:
  22.             cell.fill = openpyxl.styles.PatternFill(start_color="0B5394", fill_type="solid")
  23.             cell.font = cell.font.copy(color="ffffff")
  24.             cell.alignment = openpyxl.styles.Alignment(horizontal='center')
  25.  
  26.  
  27. def check_end(ws):
  28.     no_end = True
  29.     i = 8
  30.     while no_end:
  31.         if ws['A{}'.format(i)].value:
  32.             pass
  33.         else:
  34.             no_end = False
  35.             return i
  36.         i += 1
  37.  
  38.  
  39. def check_max_col(ws):
  40.     no_end = True
  41.     i = 8
  42.     while no_end:
  43.         if not ws['{}4'.format(get_column_letter(i))].value:
  44.             no_end = False
  45.         i += 1
  46.     return get_column_letter(i - 2)
  47.  
  48.  
  49. def check_start_a(ws):
  50.     for i in range(5, 19):
  51.         if ws['a{}'.format(i)].value:
  52.             return (i)
  53.  
  54.  
  55. def check_start_f(ws):
  56.     col_range = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
  57.     for i, n in enumerate(col_range):
  58.         if ws['{}2'.format(n)].value:
  59.             return col_range[i], col_range[i - 1]
  60.  
  61. def check_for_hide_colums(n, e, ws):
  62.     to_hide = []
  63.     col_range = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
  64.     for i, col in enumerate(col_range):
  65.         if '[Attr' in str(ws['{}{}'.format(col,n)].value):
  66.             to_hide.append(col)
  67.             # print(ws['{}{}'.format(col,n)].value)
  68.     return(to_hide)
  69.  
  70.  
  71. def hide_cols(to_hide, ws):
  72.     for col in to_hide:
  73.         ws.column_dimensions[col.upper()].hidden = True      
  74.     return ws
  75.  
  76. def check_for_hide_rows(n, m, ws):
  77.     to_hide = []
  78.     for row in range(1, n):
  79.         if '[Attr' in str(ws['{}{}'.format(m, row)].value):
  80.             to_hide.append(row)
  81.     return to_hide
  82.  
  83. def hide_rows(to_hide, ws):
  84.     for row in to_hide:
  85.         ws.row_dimensions[row].hidden = True    
  86.     return ws    
  87.  
  88. dir_list = os.listdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), "work", ""))
  89.  
  90. for filename in dir_list:
  91.     print("---------" + filename)
  92.     wb = openpyxl.load_workbook(os.path.join(os.path.dirname(os.path.realpath(__file__)), "work", filename))
  93.     for ws_name in wb.sheetnames:
  94.         ws = wb[ws_name]
  95.         for i, row in enumerate(ws.rows):
  96.             ws.column_dimensions[get_column_letter(i + 1)].width = 25
  97.         n = check_start_a(ws) # start of first block
  98.         m, e = check_start_f(ws) # m = start colum of second block; e = end colum of first block;
  99.         end = check_end(ws) # end of first block
  100.         col = check_max_col(ws) # end colum
  101.         to_hide = check_for_hide_colums(n, e, ws)
  102.         ws = hide_cols(to_hide, ws)
  103.         to_hide = check_for_hide_rows(n, m, ws)
  104.         ws = hide_rows(to_hide, ws)    
  105.         set_border(ws, 'A{}:{}{}'.format(n, e, end))
  106.         set_border(ws, '{}1:{}{}'.format(m, col, end))
  107.  
  108.         set_header(ws, 'A{}:{}{}'.format(n, e, n))
  109.         set_header(ws, '{}1:{}{}'.format(m, m, n - 1))
  110.  
  111.     wb.save(os.path.join(os.path.dirname(os.path.realpath(__file__)), "done", filename))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement