37 lines
2.5 KiB
Python
37 lines
2.5 KiB
Python
"""Extract original worksheet layout for the in-app preview without saving the template."""
|
|
from pathlib import Path
|
|
import json,warnings,openpyxl
|
|
from openpyxl.utils import get_column_letter
|
|
warnings.filterwarnings('ignore')
|
|
root=Path(__file__).resolve().parent.parent;w=openpyxl.load_workbook(root/'교각수량산출.xlsx')
|
|
layouts={};styles={}
|
|
def color(c,fallback):
|
|
if c is None:return fallback
|
|
if c.type=='rgb':return '#'+c.rgb[-6:]
|
|
if c.type=='indexed':
|
|
from openpyxl.styles.colors import COLOR_INDEX
|
|
return '#'+COLOR_INDEX[c.indexed][-6:] if c.indexed<len(COLOR_INDEX) else fallback
|
|
return fallback
|
|
from openpyxl.utils.cell import range_boundaries
|
|
for s in w:
|
|
name=s.title
|
|
area=str(s.print_area).split('!')[-1].replace('$','').replace("'",'')
|
|
firstcol,firstrow,lastcol,lastrow=range_boundaries(area)
|
|
cells={};widths=[]
|
|
for c in range(1,lastcol+1):
|
|
width=s.sheet_format.defaultColWidth or 8.43
|
|
for d in s.column_dimensions.values():
|
|
if (d.min or 0)<=c<=(d.max or d.min or 0):width=d.width
|
|
widths.append(round(width*7+5,2))
|
|
for row in s.iter_rows(min_row=firstrow,max_row=lastrow,max_col=lastcol):
|
|
for c in row:
|
|
border={}
|
|
for side in ['top','bottom','left','right']:
|
|
b=getattr(c.border,side)
|
|
if b and b.style:border[side]={'width':2 if b.style in ['medium','thick','double'] else 1,'style':'dashed' if b.style in ['dashed','dotted','dashDot'] else 'solid','color':color(b.color,'#222222')}
|
|
style={'font':c.font.name or 'Malgun Gothic','size':c.font.sz or 8,'bold':bool(c.font.bold),'italic':bool(c.font.italic),'color':color(c.font.color,'#111111'),'background':color(c.fill.fgColor,'#ffffff') if c.fill.patternType=='solid' else '#ffffff','align':c.alignment.horizontal or ('right' if isinstance(c.value,(int,float)) else 'left'),'valign':c.alignment.vertical or 'center','border':border,'wrap':bool(c.alignment.wrap_text)}
|
|
if c.value is not None or c.has_style:
|
|
styles[c.style_id]=style;cells[c.coordinate]=c.style_id
|
|
layouts[name]={'firstRow':firstrow,'rows':lastrow,'cols':lastcol,'widths':widths,'heights':[round((s.row_dimensions[r].height or s.sheet_format.defaultRowHeight or 15)*4/3,2) for r in range(1,lastrow+1)],'merges':[[m.min_row,m.min_col,m.max_row,m.max_col] for m in s.merged_cells.ranges if m.min_row<=lastrow and m.min_col<=lastcol],'cells':cells}
|
|
(root/'template-layout.ts').write_text('export const sheetLayouts='+json.dumps(layouts,ensure_ascii=False,separators=(',',':'))+';\nexport const excelStyles='+json.dumps(styles,ensure_ascii=False,separators=(',',':'))+';\n',encoding='utf8')
|