Commit 87ab47a0 authored by yanke's avatar yanke
Browse files

fix bug

parent b104ec49
......@@ -18,6 +18,16 @@
},
"column_add": {
}
},
{
"file": "bw100.xlsx",
"sheets": [],
"column_mapping": {
},
"column_replace": {
},
"column_add": {
}
}
]
}
......@@ -17,37 +17,55 @@ args = parser.parse_args()
def fill_merged_cells(df, file_path, sheet_name):
"""填充合并单元格:用前一个非空值向下填充"""
merged_cols = set()
try:
wb = pd.ExcelFile(file_path).book
from openpyxl import load_workbook
wb = load_workbook(file_path, data_only=True)
ws = wb[sheet_name]
merged_ranges = ws.merged_cells.ranges
if not merged_ranges:
wb.close()
return df
for merged_range in merged_ranges:
min_col, min_row = merged_range.min_col, merged_range.min_row
max_col, max_row = merged_range.max_col, merged_range.max_row
first_cell = ws.cell(min_row, min_col).value
for col_idx in range(min_col, max_col + 1):
merged_cols.add(col_idx - 1)
wb.close()
except Exception as e:
print(f" 检测合并单元格失败: {e}")
return df
for merged_range in merged_ranges:
min_col, min_row = merged_range.min_col, merged_range.min_row
max_col, max_row = merged_range.max_col, merged_range.max_row
for row in range(min_row + 1, max_row + 1):
for col in range(min_col, max_col + 1):
ws.cell(row, col).value = first_cell
for col_idx in range(min_col, max_col + 1):
merged_cols.add(col_idx - 1)
df = pd.read_excel(file_path, sheet_name=sheet_name, engine='openpyxl')
except Exception:
pass
wb.close(wb)
except Exception as e:
print(f" 检测合并单元格失败: {e}")
return df
if not merged_cols:
return df
for col in df.columns:
df[col] = df[col].fillna(method='ffill')
if pd.api.types.is_numeric_dtype(df[col]):
col_values = df[col].dropna()
if len(col_values) > 0 and col_values.apply(lambda x: float(x).is_integer() if pd.notna(x) else True).all():
try:
df[col] = df[col].astype(int)
except (ValueError, TypeError):
pass
for i, col in enumerate(df.columns):
if i in merged_cols:
df[col] = df[col].fillna(method='ffill')
if pd.api.types.is_numeric_dtype(df[col]):
col_values = df[col].dropna()
if len(col_values) > 0 and col_values.apply(lambda x: float(x).is_integer() if pd.notna(x) else True).all():
try:
df[col] = df[col].astype(int)
except (ValueError, TypeError):
pass
return df
......@@ -498,7 +516,7 @@ all_metric_cols = normal_metrics + [c['column'] for c in normalize_configs]
print(f"\n普通指标: {normal_metrics}")
print(f"归一化配置: {normalize_configs}")
dist_combinations = df_renamed.groupby(dist_cols).size().reset_index()
dist_combinations = df_renamed.groupby(dist_cols).size().reset_index(name='count')
print(f"\n将生成 {len(dist_combinations)} 个图表...")
chart_count = 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment