schema_excel.py 4.3 KB

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