读取指定路径的 Document IR JSON,并统计章节/图表数量。 解析失败时返回 None;成功时会打印章节数与图表数,便于确认 输入报告的规模。 参数: file_path: IR 文件路径 返回: dict | None: 解析后的 Document IR;失败返回 None。
(file_path)
| 41 | return latest_file |
| 42 | |
| 43 | def load_document_ir(file_path): |
| 44 | """ |
| 45 | 读取指定路径的 Document IR JSON,并统计章节/图表数量。 |
| 46 | |
| 47 | 解析失败时返回 None;成功时会打印章节数与图表数,便于确认 |
| 48 | 输入报告的规模。 |
| 49 | |
| 50 | 参数: |
| 51 | file_path: IR 文件路径 |
| 52 | |
| 53 | 返回: |
| 54 | dict | None: 解析后的 Document IR;失败返回 None。 |
| 55 | """ |
| 56 | try: |
| 57 | with open(file_path, 'r', encoding='utf-8') as f: |
| 58 | document_ir = json.load(f) |
| 59 | |
| 60 | logger.info(f"成功加载报告: {file_path.name}") |
| 61 | |
| 62 | # 统计图表数量 |
| 63 | chart_count = 0 |
| 64 | chapters = document_ir.get('chapters', []) |
| 65 | |
| 66 | def count_charts(blocks): |
| 67 | """递归统计 block 列表中的 Chart.js 图表数量""" |
| 68 | count = 0 |
| 69 | for block in blocks: |
| 70 | if isinstance(block, dict): |
| 71 | if block.get('type') == 'widget' and block.get('widgetType', '').startswith('chart.js'): |
| 72 | count += 1 |
| 73 | # 递归处理嵌套blocks |
| 74 | nested = block.get('blocks') |
| 75 | if isinstance(nested, list): |
| 76 | count += count_charts(nested) |
| 77 | return count |
| 78 | |
| 79 | for chapter in chapters: |
| 80 | blocks = chapter.get('blocks', []) |
| 81 | chart_count += count_charts(blocks) |
| 82 | |
| 83 | logger.info(f"报告包含 {len(chapters)} 个章节,{chart_count} 个图表") |
| 84 | |
| 85 | return document_ir |
| 86 | |
| 87 | except Exception as e: |
| 88 | logger.error(f"加载报告失败: {e}") |
| 89 | return None |
| 90 | |
| 91 | def generate_pdf_with_vector_charts(document_ir, output_path, ir_file_path=None): |
| 92 | """ |
no test coverage detected