|
楼主 |
发表于 2024-5-31 09:56:35
|
显示全部楼层
class BeautifySheet:
def __init__(self):
pass
def fun_set_excel(self,excel):
self.excel = excel
def fun_beautify(self,sheet_name,dict):
self.sheet = self.excel[sheet_name]
self.column_styles = dict
for self.column in self.sheet.columns:
self.column_letter = self.column[0].column_letter
print(self.column_letter)#即ABCD.....
if self.column_letter in self.column_styles:
self.style = self.column_styles[self.column_letter]
self.sheet.column_dimensions[self.column_letter].width = self.style["width"]#设置列宽
for self.cell in self.column:
self.cell.border = self.style["border"]#边框
self.cell.font = self.style["font"]#字体
self.alignment = self.style["alignment"]#居中方式
from openpyxl.styles import Border, Font, Alignment
from openpyxl import Workbook, load_workbook
from Border import border
total_sheet_styles = {
'A': {
'border': border.l_thick_d_thin,
'font': Font(size=12, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'B': {
'border': border.l_thin_d_thin,
'font': Font(size=15, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 12
},
'C': {
'border': border.l_drop_d_thin,
'font': Font(size=12, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 12
},
'D': {
'border': border.l_thick_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'E': {
'border': border.d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 6
},
'F': {
'border': border.l_drop_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'G': {
'border': border.d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'H': {
'border': border.l_thin_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'I': {
'border': border.d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 6
},
'J': {
'border': border.l_drop_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'K': {
'border': border.l_thin_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'L': {
'border': border.d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'M': {
'border': border.d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'N': {
'border': border.l_thin_d_thin,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
},
'O': {
'border': border.d_thin_r_thick,
'font': Font(size=10, bold=True),
'alignment': Alignment(horizontal='center', vertical='center',wrap_text=True),
'width' : 10
}
}
为什么表格传入后美化~~单元格不会换行显示
|
|