certflow.handlers.excel_styler module¶
Excel 样式处理器模块
提供单元格颜色、字体、批注等格式信息的读取功能。
- class certflow.handlers.excel_styler.ExcelStyler[源代码]¶
基类:
objectExcel 样式处理器
负责读取 Excel 单元格的格式信息: - 背景色 - 字体色 - 批注内容 - 其他样式属性
- static open_workbook(file_path, data_only=True)[源代码]¶
打开工作簿的上下文管理器
使用上下文管理器自动管理Excel工作簿的打开和关闭, 确保资源正确释放。
- 参数:
- 生成器:
Workbook -- openpyxl工作簿对象
- 返回类型:
Iterator[Workbook]
示例
>>> with ExcelStyler.open_workbook(Path("data.xlsx")) as wb: ... ws = wb.active ... cell = ws["A1"] ... print(cell.value)
- static get_cell_comment(cell)[源代码]¶
获取单元格批注内容
- 参数:
cell (Cell) -- openpyxl单元格对象
- 返回:
批注文本内容,没有批注时返回None
- 返回类型:
Optional[str]
示例
>>> with ExcelStyler.open_workbook(Path("data.xlsx")) as wb: ... ws = wb.active ... comment = ExcelStyler.get_cell_comment(ws["A1"]) ... if comment: ... print(f"批注内容: {comment}")
- static get_cell_style_info(cell)[源代码]¶
获取单元格完整样式信息
一次性获取单元格的值、背景色、字体色、状态、批注和字体属性。
- 参数:
cell (Cell) -- openpyxl单元格对象
- 返回:
- 包含以下键的字典:
value: 单元格值
bg_color: 背景色
font_color: 字体色
status: 根据颜色推断的业务状态
comment: 批注内容
font_name: 字体名称
font_size: 字体大小
bold: 是否粗体
italic: 是否斜体
- 返回类型:
Dict[str, Any]
示例
>>> with ExcelStyler.open_workbook(Path("data.xlsx")) as wb: ... ws = wb.active ... info = ExcelStyler.get_cell_style_info(ws["A1"]) ... print(f"值: {info['value']}, 状态: {info['status']}")
- static read_with_styles(file_path, sheet_name=0, include_styles=True, include_comments=True)[源代码]¶
读取 Excel 并返回数据和样式信息
同时读取单元格的值和样式信息,返回多个DataFrame。
- 参数:
- 返回:
- 包含5个元素的元组:
df_data: 数据值DataFrame
bg_colors: 背景色DataFrame(可选)
font_colors: 字体色DataFrame(可选)
comments: 批注DataFrame(可选)
statuses: 状态DataFrame(可选)
- 返回类型:
Tuple
示例
>>> data, bg, font, comments, status = ExcelStyler.read_with_styles( ... Path("data.xlsx"), ... sheet_name="Sheet1" ... ) >>> print(f"数据形状: {data.shape}") >>> print(f"状态统计: {status.iloc[0, 0]}")
- static get_comments_summary(file_path)[源代码]¶
获取批注汇总表
扫描整个工作簿,返回所有批注的结构化摘要。
- 参数:
file_path (Path) -- Excel文件路径
- 返回:
- 包含以下列的DataFrame:
工作表: 批注所在工作表名称
单元格: 批注所在单元格坐标(如"A1")
批注内容: 批注的文本内容
- 返回类型:
pd.DataFrame
示例
>>> summary = ExcelStyler.get_comments_summary(Path("data.xlsx")) >>> print(summary) >>> # 输出示例: >>> # 工作表 单元格 批注内容 >>> # 0 Sheet1 A1 请注意此单元格 >>> # 1 Sheet1 B5 需要审核
- static get_all_comments(file_path)[源代码]¶
获取整个工作簿的所有批注
遍历所有工作表和单元格,收集所有批注信息。
- 参数:
file_path (Path) -- Excel文件路径
- 返回:
- 嵌套字典结构
外层键为工作表名称,内层键为单元格坐标,值为批注内容
- 返回类型:
示例
>>> all_comments = ExcelStyler.get_all_comments(Path("data.xlsx")) >>> for sheet, cells in all_comments.items(): ... print(f"工作表: {sheet}") ... for cell, comment in cells.items(): ... print(f" {cell}: {comment}")
- static read_with_comments_merged(file_path, sheet_name=0, comment_column=None)[源代码]¶
读取数据并将指定列的批注合并为新列
- static get_cell_border(cell)[源代码]¶
获取单元格四边边框信息
- 参数:
cell (Cell) -- openpyxl 单元格对象
- 返回:
包含 top/bottom/left/right 的字典,每条边为 {"style": ..., "color": ...};无任何边框时返回 None
- 返回类型:
Optional[Dict]
- static add_border_to_range(ws, min_row, min_col, max_row, max_col, style='thin', color='000000')[源代码]¶
为矩形区域的所有单元格添加统一边框