schema_excel.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from dataclasses import dataclass, field
  2. from typing import List, Literal
  3. from openpyxl.styles import PatternFill,Alignment,Font
  4. from pydantic import Field
  5. def ExcelField(
  6. name:str,
  7. cell_type:Literal['numeric', 'string', 'image'] = "string",
  8. width:int=16,
  9. height:int=14,
  10. default:str='',
  11. converter:str='',
  12. prompt:str='',
  13. combo:List=[],
  14. date_format:str='',
  15. is_export:bool=True,
  16. is_statistics:bool=False,
  17. background_color:str='FFFFFFFF',
  18. color:str='FF000000',
  19. align:str='left',
  20. action:Literal['import', 'export', 'both']='both',
  21. dict_type:str=''
  22. ):
  23. excel_access = ExcelAccess(
  24. name=name,
  25. cell_type=cell_type,
  26. width=width,
  27. height=height,
  28. default=default,
  29. converter=converter,
  30. prompt=prompt,
  31. date_format=date_format,
  32. combo=combo,
  33. is_export=is_export,
  34. is_statistics=is_statistics,
  35. background_color=background_color,
  36. color=color,
  37. align=align,
  38. action=action,
  39. dict_type=dict_type
  40. )
  41. return Field(excel_access=excel_access)
  42. def ExcelFields(*accesses:"ExcelAccess"):
  43. return Field(excel_access=accesses)
  44. @dataclass
  45. class ExcelAccess:
  46. # 导出时在excel中排序
  47. sort: int = 0
  48. # 导出到Excel中的名字
  49. name: str = ''
  50. # 日期格式, 如: yyyy-MM-dd
  51. date_format: str = ''
  52. # 字典类型,请设置字典的type值 (如: sys_user_sex)
  53. dict_type: str = ''
  54. # 读取内容转表达式 (如: 0=男,1=女,2=未知)
  55. converter: str = ''
  56. # 分隔符,读取字符串组内容
  57. separators: str = ','
  58. # Decimal 精度 默认:False(默认不开启Decimal格式化)
  59. scale: bool = False
  60. # Decimal 舍入规则
  61. roundmode: str = ''
  62. # 导出时在excel中每个行的高度 单位为字符
  63. height: int = 14
  64. # 导出时在excel中每个列的宽 单位为字符
  65. width: int = 16
  66. # 文字后缀,如% 90 变成90%
  67. suffix: str = ''
  68. # 当值为空时,字段的默认值
  69. default: str = ''
  70. # 提示信息
  71. prompt: str = ''
  72. # 设置只能选择不能输入的列内容.
  73. combo: List = field(default_factory=list)
  74. # 另一个类中的属性名称,支持多级获取,以小数点隔开
  75. attr: str = ''
  76. # 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
  77. is_export: bool = True
  78. # 是否自动统计数据,在最后追加一行统计数据总和
  79. is_statistics : bool = False
  80. # 导出类型(numeric 数字 string 字符串 image 图片)
  81. cell_type: Literal['numeric', 'string', 'image'] = 'string'
  82. # # 表头背景色
  83. # header_background_color: str = 'FFF2F2F2'
  84. # # 表头文字颜色
  85. # header_color: str = 'FF000000'
  86. # 单元格背景色
  87. background_color: str = 'FFFFFFFF'
  88. # 单元格文字颜色
  89. color: str = 'FF000000'
  90. # 导出字段对齐方式(left:默认;left:靠左;center:居中;right:靠右)
  91. align: Literal['left', 'center', 'right'] = 'left'
  92. # 自定义数据处理器
  93. # handler: str = ''
  94. # 自定义数据处理器参数
  95. # args: List = field(default_factory=list)
  96. # 字段类型(both:导出导入;export:仅导出;import:仅导入)
  97. action: Literal['import', 'export', 'both'] = 'both'
  98. val: str = field(default=None, init=False)
  99. def __post_init__(self):
  100. self._fill = PatternFill(
  101. start_color=self.background_color,
  102. end_color=self.background_color,
  103. )
  104. self._align = Alignment(
  105. horizontal=self.align,
  106. vertical='center',
  107. text_rotation=0,
  108. wrap_text=False,
  109. shrink_to_fit=False,
  110. indent=0
  111. )
  112. self._header_font = Font(
  113. bold=True,
  114. size=12
  115. )
  116. self._row_font = Font(
  117. size=12
  118. )
  119. @property
  120. def fill(self):
  121. return self._fill
  122. @property
  123. def alignment(self):
  124. return self._align
  125. @property
  126. def header_font(self):
  127. return self._header_font
  128. @property
  129. def row_font(self):
  130. return self._row_font