|
|
@@ -13,7 +13,7 @@ from utils.file import read_file
|
|
|
from utils.logu import logger
|
|
|
|
|
|
# 样式常量
|
|
|
-RED_FILL = PatternFill(start_color='FFFF0000', fill_type='solid')
|
|
|
+RED_FILL = PatternFill(start_color='FF0000',end_color="FF0000", fill_type='solid') # 修正为RGB格式
|
|
|
HEADER_FONT = Font(bold=True, color='FFFFFF')
|
|
|
HEADER_FILL = PatternFill(start_color='4F81BD', patternType='solid')
|
|
|
COLUMN_SPACING = 3 # 每个产品占3列(关键词、搜索量、空列)
|
|
|
@@ -70,6 +70,7 @@ class ExcelGenerator:
|
|
|
self.output_path = Path(output_path)
|
|
|
self.current_col = 1
|
|
|
self.max_data_rows = 0 # 记录最大数据行数
|
|
|
+ self.product_cols = [] # 记录所有产品起始列
|
|
|
|
|
|
def add_product(self, json_path: str, asin: str):
|
|
|
"""添加产品数据"""
|
|
|
@@ -78,6 +79,9 @@ class ExcelGenerator:
|
|
|
data = json.loads(read_file(json_path))
|
|
|
processor = ProductDataProcessor(data, asin)
|
|
|
|
|
|
+ # 记录产品起始列
|
|
|
+ self.product_cols.append(self.current_col)
|
|
|
+
|
|
|
# 写入主数据表
|
|
|
self._write_main_table(processor, asin)
|
|
|
|
|
|
@@ -123,11 +127,12 @@ class ExcelGenerator:
|
|
|
kw_cell.font = Font(color='0000FF', underline='single') # 添加蓝色下划线样式
|
|
|
|
|
|
# 搜索量
|
|
|
- search_cell = self.ws.cell(data_row, self.current_col + 1, row['monthly_searches'])
|
|
|
- search_cell.number_format = '#,##0'
|
|
|
+ search_cell = self.ws.cell(data_row, self.current_col + 1, int(row['monthly_searches']))
|
|
|
+ search_cell.number_format = 'General'
|
|
|
+ search_cell.value = int(search_cell.value) # 确保存储为整数类型
|
|
|
|
|
|
# 更新最大行数
|
|
|
- self.max_data_rows = max(self.max_data_rows, len(df) + 1)
|
|
|
+ self.max_data_rows = max(self.max_data_rows, len(df) + 2) # 修正最大行号计算
|
|
|
|
|
|
# 设置初始列宽
|
|
|
self.ws.column_dimensions[get_column_letter(self.current_col)].width = 35
|
|
|
@@ -183,22 +188,28 @@ class ExcelGenerator:
|
|
|
|
|
|
def _apply_conditional_formatting(self):
|
|
|
"""应用条件格式"""
|
|
|
+ # 修正颜色定义(使用RGB格式)
|
|
|
+
|
|
|
+ # 创建条件格式规则(移除字体设置)
|
|
|
red_rule = CellIsRule(
|
|
|
operator='greaterThan',
|
|
|
- formula=['10000'], # 1万阈值
|
|
|
+ formula=['10000'],
|
|
|
+ stopIfTrue=True,
|
|
|
fill=RED_FILL
|
|
|
)
|
|
|
|
|
|
- # 精确应用条件格式到每个产品的搜索量列(current_col+1)
|
|
|
- # 遍历每个产品的搜索量列(current_col +1 的步进)
|
|
|
- for col in range(1, self.current_col, COLUMN_SPACING):
|
|
|
- search_col = col + 1 # 每个产品的搜索量列
|
|
|
- col_letter = get_column_letter(search_col)
|
|
|
- # 应用格式到整个数据区域
|
|
|
- self.ws.conditional_formatting.add(
|
|
|
- f'{col_letter}3:{col_letter}{self.max_data_rows}',
|
|
|
- red_rule
|
|
|
- )
|
|
|
+ # 计算目标列字母(B=2, E=5, H=8...)
|
|
|
+ target_columns = []
|
|
|
+ # 使用记录的product_cols计算目标列
|
|
|
+ for start_col in self.product_cols:
|
|
|
+ search_col = start_col + 1 # 搜索量列是起始列+1
|
|
|
+ target_columns.append(get_column_letter(search_col))
|
|
|
+
|
|
|
+ # 应用条件格式到所有目标列
|
|
|
+ for col_letter in target_columns:
|
|
|
+ cell_range = f"{col_letter}3:{col_letter}{self.max_data_rows}"
|
|
|
+ self.ws.conditional_formatting.add(cell_range, red_rule)
|
|
|
+ logger.info(f"应用条件格式到 {cell_range} (值 > 10000)")
|
|
|
|
|
|
def _adjust_column_widths(self):
|
|
|
"""自动调整列宽"""
|