python 生成复杂表格,自动分页等功能

news2025/7/22 22:18:57

python 生成复杂表格,自动分页等功能

解决将Python中的树形目录数据转换为Word表格,并生成带有合并单元格的检测报告的问题。首先,要解决“tree目录数据”和“Word表格互换”,指将树形结构的数据转换成Word表格,并在转换过程中合并相应的单元格以反映层次结构。此外,还有自动分页和固定表头的问题,现在可能希望生成的报告能够自动处理这些格式问题。

首先,我需要明确树形目录数据的结构。通常,树形结构有父子节点关系,比如文件夹结构,每个节点可能有名称、类型、大小、修改日期等属性。希望将这些层次结构在Word表格中展示,通过合并单元格来表示父子关系,例如,一个父节点占据一行,其子节点在下方缩进显示,可能需要合并某些列来体现层级。

在Python中,处理树形结构通常使用递归。对于每个节点,可能需要确定其在表格中的行位置,并根据层级决定合并哪些单元格。例如,根节点可能需要合并所有列的一行,子节点可能只在第一列显示名称,后面的列合并或留空,或者根据具体需求调整。

生成Word文档需要使用python-docx库。该库允许创建表格、设置样式、合并单元格等操作。关键点是如何遍历树形数据并动态生成表格行,同时处理合并单元格的逻辑。可能需要为每个节点计算其在表格中的行数和列数,特别是当子节点存在时,父节点可能需要跨多行合并。

以下是试验代码

from docx import Document
from docx.shared import Pt, Cm
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml.shared import OxmlElement, qn
import random


class TreeNode:
    def __init__(self, name, depth=0, is_file=False, size=0, parent=None):
        self.name = name
        self.depth = depth
        self.is_file = is_file
        self.size = f"{size} KB" if is_file else ""
        self.parent = parent
        self.children = []
        self.start_row = 0
        self.end_row = 0
        self.col_span = 1  # 新增横向合并跨度


class EnhancedDirectoryReport:
    def __init__(self, filename):
        self.doc = Document()
        self.filename = filename
        self._setup_document()
        self.table = None
        self.current_row = 0
        self.column_map = ['一级目录', '二级目录', '三级目录', '文件名', '路径', '大小']

        self.current_table = None
        self.current_page_rows = 0
        self.max_page_rows = 35  # 根据实际内容调整每页行数
        self.active_directory = {}  # 记录当前活跃的目录层级

    def _setup_document(self):
        section = self.doc.sections[0]
        margins = {'left': 2, 'right': 2, 'top': 2.5, 'bottom': 2.5}
        for attr, cm_val in margins.items():
            setattr(section, f"{attr}_margin", Cm(cm_val))

        style = self.doc.styles['Normal']
        style.font.name = '微软雅黑'
        style.font.size = Pt(10)

    def _create_new_page(self):
        """创建新页面并初始化表格"""
        if self.current_table is not None:
            self.doc.add_page_break()

        self.current_table = self.doc.add_table(rows=0, cols=6)
        self.current_table.style = 'Table Grid'
        widths = [Cm(3.5), Cm(3.5), Cm(3.5), Cm(4), Cm(6), Cm(2.5)]
        for idx, w in enumerate(widths):
            self.current_table.columns[idx].width = w

        self._create_table_header()
        print('建表头后',self.current_row,self.current_page_rows)
        self.current_page_rows = 1  # 表头占1行

        # 重新应用活跃目录
        self._reapply_active_directory()

    def _reapply_active_directory(self):
        """在新页重新应用当前活跃目录"""
        for depth in [1, 2, 3]:
            if depth in self.active_directory:
                node = self.active_directory[depth]
                self._add_directory_row(node, depth)

    def _add_directory_row(self, node, depth):
        """添加目录行并更新活跃状态"""
        row = self.current_table.add_row()
        cells = row.cells

        # 填充目录信息
        cells[depth - 1].text = node.name
        cells[depth - 1].paragraphs[0].alignment = WD_TABLE_ALIGNMENT.LEFT

        # 设置跨列合并
        if depth == 1:
            cells[0].merge(cells[5])
        elif depth == 2:
            cells[1].merge(cells[5])
        elif depth == 3:
            cells[2].merge(cells[5])

        # 更新活跃目录
        self.active_directory[depth] = node
        self.current_page_rows += 1

    def _check_page_break(self):
        """检查是否需要分页"""
        if self.current_page_rows >= self.max_page_rows:
            self._create_new_page()
            print('分页')

    def _add_file_row(self, node):
        """添加文件行"""
        self._check_page_break()

        row = self.current_table.add_row()
        cells = row.cells

        # 填充文件信息
        cells[3].text = node.name
        cells[4].text = self._get_full_path(node)
        cells[5].text = node.size

        # 继承活跃目录
        for depth in [1, 2, 3]:
            if depth in self.active_directory:
                cells[depth - 1].text = self.active_directory[depth].name
                cells[depth - 1].paragraphs[0].alignment = WD_TABLE_ALIGNMENT.CENTER

        self.current_page_rows += 1

    def _get_full_path(self, node):
        path = []
        current = node.parent
        while current and current.depth > 0:
            path.insert(0, current.name)
            current = current.parent
        return '/' + '/'.join(path)

    def process_structure(self, root):
        """处理目录结构"""
        self._create_new_page()

        stack = [(root, False)]  # (node, visited)
        while stack:
            node, visited = stack.pop()
            if visited:
                # 后序遍历处理合并
                if not node.is_file:
                    self._update_active_directory(node)
                continue

            if node.is_file:
                self._add_file_row(node)
            else:
                # 前序遍历添加目录
                self._check_page_break()
                self._add_directory_row(node, node.depth)
                stack.append((node, True))

            # 逆向添加子节点以保持顺序
            for child in reversed(node.children):
                stack.append((child, False))

        self.doc.save(self.filename)

    def _update_active_directory(self, node):
        """更新活跃目录状态"""
        # 清除子目录状态
        for depth in list(self.active_directory.keys()):
            if depth > node.depth:
                del self.active_directory[depth]

    def _create_table_header(self):
        header = self.table.add_row()
        for idx, text in enumerate(self.column_map):
            cell = header.cells[idx]
            cell.text = text
            cell.paragraphs[0].runs[0].font.bold = True
            cell.paragraphs[0].alignment = WD_TABLE_ALIGNMENT.CENTER
            self._set_cell_color(cell, 'A3D3D3')

        tr = header._tr
        trPr = tr.get_or_add_trPr()
        tblHeader = OxmlElement('w:tblHeader')
        tblHeader.set(qn('w:val'), "true")
        trPr.append(tblHeader)
        print(self.current_row)
        self.current_row += 1

    def _set_cell_color(self, cell, hex_color):
        shading = OxmlElement('w:shd')
        shading.set(qn('w:fill'), hex_color)
        cell._tc.get_or_add_tcPr().append(shading)

    def _smart_merge(self, node):
        """智能合并策略核心方法"""
        # 垂直合并处理
        if node.depth <= 3 and not node.is_file:
            self._vertical_merge(node)
        # 横向合并处理
        if node.depth == 1 and not any(not c.is_file for c in node.children):
            self._horizontal_merge(node, 1, 3)  # 一级目录合并到文件名列

        if node.depth == 2 and not any(not c.is_file for c in node.children):
            self._horizontal_merge(node, 2, 3)  # 二级目录合并到文件名列

    def _horizontal_merge(self, node, start_col, end_col):
        """安全横向合并方法"""
        for row_idx in range(node.start_row, node.end_row):
            # 获取需要合并的单元格
            print('nc ', row_idx, start_col, end_col)
            start_cell = self.table.cell(row_idx, start_col)
            end_cell = self.table.cell(row_idx, end_col)
            print(row_idx, start_col, end_col)
            print('开结',start_cell, end_cell)
            # 检查是否已经被合并
            if start_cell._element is end_cell._element:
                print('已合并过')
                continue
            else:
                start_cell.merge(end_cell)

    def _vertical_merge(self, node):
        """垂直方向合并"""
        if node.start_row >= node.end_row:
            return

        depth_col_map = {1: 0, 2: 1, 3: 2}
        col_idx = depth_col_map.get(node.depth)
        if col_idx is not None:
            try:
                start_cell = self.table.cell(node.start_row, col_idx)
                end_cell = self.table.cell(node.end_row - 1, col_idx)
                start_cell.merge(end_cell)
                start_cell.text = node.name
            except IndexError as e:
                print(f"垂直合并失败:{node.name}")
                raise e


    def _fill_row_data(self, node):
        """填充数据并设置合并策略"""
        row = self.table.add_row()
        cells = row.cells



        # 文件信息
        if node.is_file:
            cells[3].text = node.name
            cells[4].text = self._get_full_path(node)
            cells[5].text = node.size
        # else:
        #     # 设置目录层级
        #     for d in range(1, 4):
        #         print(d, cells[d])
        #         print(node.name, node.depth)
        #         if node.depth == d:
        #             cells[d - 1].text = node.name
        #             # if d < 3:
        #             #     cells[d].merge(cells[d])

        # 设置样式
        for cell in cells:
            cell.vertical_alignment = WD_TABLE_ALIGNMENT.CENTER

        self.current_row += 1
        return row

    # def _get_full_path(self, node):
    #     path = []
    #     current = node.parent
    #     while current and current.depth > 0:
    #         path.insert(0, current.name)
    #         current = current.parent
    #     return '/' + '/'.join(path) + ('' if node.is_file else f'/{node.name}')

    def _process_node(self, node):
        node.start_row = self.current_row
        #增限制,如为净空不加行
        if node.depth > 1 and node.is_file:
            self._fill_row_data(node)

        for child in node.children:
            self._process_node(child)

        node.end_row = self.current_row
        self._smart_merge(node)

    def generate_report(self, root):
        self.table = self.doc.add_table(rows=0, cols=6)
        self.table.style = 'Table Grid'
        widths = [Cm(3.5), Cm(3.5), Cm(3.5), Cm(4), Cm(6), Cm(2.5)]
        for idx, w in enumerate(widths):
            self.table.columns[idx].width = w

        # self._create_table_header()
        self._create_new_page()
        self._process_node(root)
        print(self.doc.tables)
        self.doc.save(self.filename)


# 测试数据生成器
class TestDataGenerator:
    @staticmethod
    def create_large_structure():
        root = TreeNode("ROOT", depth=0)

        # 一级目录(10个)
        for i in range(1, 11):
            dir1 = TreeNode(f"一级目录_{i}", depth=1, parent=root)
            root.children.append(dir1)

            # 30%概率没有子目录
            if random.random() < 0.3:
                # 直接添加文件
                for j in range(random.randint(2, 5)):
                    file = TreeNode(f"文件_{i}-{j}.docx", depth=4,
                                    is_file=True,
                                    size=random.randint(100, 5000),
                                    parent=dir1)
                    dir1.children.append(file)
                continue

            # 二级目录(每个一级目录3-5个)
            for j in range(random.randint(3, 5)):
                dir2 = TreeNode(f"二级目录_{i}-{j}", depth=2, parent=dir1)
                dir1.children.append(dir2)

                # 50%概率没有三级目录
                if random.random() < 0.5:
                    # 直接添加文件
                    for k in range(random.randint(3, 6)):
                        file = TreeNode(f"文件_{i}-{j}-{k}.xlsx", depth=4,
                                        is_file=True,
                                        size=random.randint(100, 5000),
                                        parent=dir2)
                        dir2.children.append(file)
                    continue

                # 三级目录(每个二级目录2-4个)
                for k in range(random.randint(2, 4)):
                    dir3 = TreeNode(f"三级目录_{i}-{j}-{k}", depth=3, parent=dir2)
                    dir2.children.append(dir3)

                    # 添加文件
                    for m in range(random.randint(3, 8)):
                        file = TreeNode(f"文件_{i}-{j}-{k}-{m}.pptx", depth=4,
                                        is_file=True,
                                        size=random.randint(100, 5000),
                                        parent=dir3)
                        dir3.children.append(file)
        return root


if __name__ == '__main__':
    # 生成测试数据
    data_generator = TestDataGenerator()
    root_node = data_generator.create_large_structure()

    # 生成报告
    report = EnhancedDirectoryReport("上下左右目录2.docx")
    report.generate_report(root_node)

效果如图所示:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2387859.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【文献阅读】EndoChat: Grounded Multimodal Large Language Model for Endoscopic Surgery

[2501.11347] EndoChat: Grounded Multimodal Large Language Model for Endoscopic Surgery 2025年1月 数据可用性 Surg-396K 数据集可在 GitHub - gkw0010/EndoChat 公开获取。 代码可用性 EndoChat 的代码可在 GitHub - gkw0010/EndoChat 下载。 摘要 近年来&#xff…

React JSX语法介绍(JS XML)(一种JS语法扩展,允许在JS代码中编写类似HTML的标记语言)Babel编译

在线调试网站&#xff1a;https://zh-hans.react.dev/learn 文章目录 JSX&#xff1a;现代前端开发的声明式语法概述JSX的本质与工作原理什么是JSXJSX转换流程 JSX语法特性表达式嵌入&#xff08;JSX允许在大括号内嵌入任何有效的JavaScript表达式&#xff09;属性传递&#xf…

【R语言编程绘图-箱线图】

基本箱线图绘制 使用ggplot2绘制箱线图的核心函数是geom_boxplot()。以下是一个基础示例&#xff0c;展示如何用iris数据集绘制不同物种&#xff08;Species&#xff09;的萼片长度&#xff08;Sepal.Length&#xff09;分布&#xff1a; library(ggplot2) ggplot(iris, aes(…

MongoDB索引:原理、实践与优化指南

为什么索引对数据库如此重要&#xff1f; 在现代应用开发中&#xff0c;数据库性能往往是决定用户体验的关键因素。想象一下&#xff0c;当你在电商平台搜索商品时&#xff0c;如果每次搜索都需要等待5-10秒才能看到结果&#xff0c;这种体验是多么令人沮丧。MongoDB作为最流行…

SQL实战之索引优化(单表、双表、三表、索引失效)

文章目录 单表优化双表优化三表优化结论索引失效 单表优化 总体原则&#xff1a;建立索引并合理使用&#xff0c;避免索引失效 案例说明&#xff1a;查询category_ id 为1且comments大于1的情况下,views最多的article_ id: 传统方案&#xff1a; explain select id, author_ id…

[7-1] ADC模数转换器 江协科技学习笔记(14个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 DMA&#xff08;Direct Memory Access&#xff0c;直接内存访问&#xff09;是一种硬件特性&#xff0c;它允许某些硬件子系统直接访问系统的内存&#xff0c;而无需CPU的介入。这样&#xff0c;CPU就可以处理其他任务&#xff0c;从而提高系…

阿里云 Serverless 助力海牙湾构建弹性、高效、智能的 AI 数字化平台

作者&#xff1a;赵世振、十眠、修省 “通过阿里云 Serverless 架构&#xff0c;我们成功解决了弹性能力不足、资源浪费与运维低效的痛点。SAE 的全托管特性大幅降低技术复杂度。未来&#xff0c;我们将进一步探索 Serverless 与 AI 的结合&#xff0c;为客户提供更智能的数字…

一个开源的多播放源自动采集在线影视网站

这里写自定义目录标题 欢迎使用Markdown编辑器GoFilm简介项目部署1、前置环境准备1.2 redis 配置 film-api 后端服务配置将 GoFilm 项目根目录下的 film 文件夹上传到 linux 服务器的 /opt 目录下 2. 构建运行1. docker 部署1.1 安装 docker , docker compose 环境 注意事项: 2…

深度检测与动态透明度控制 - 基于Babylon.js的遮挡检测实现解析

首先贴出实现代码&#xff1a; OcclusionFader.ts import { AbstractEngine, Material, type Behavior, type Mesh, type PBRMetallicRoughnessMaterial, type Scene } from "babylonjs/core"; import { OcclusionTester } from "../../OcclusionTester"…

docker push 报错 denied: requested access to the resource is denied

问题&#xff1a;当 docker logout -> docker login 用户登录&#xff0c;但仍然无法 docker push $ docker push <username>/nginx-custom:v1 The push refers to repository [docker.io/jagger/nginx-custom] 340e6d3ea0c7: Preparing 941dd9dd8ee4: Preparing f6…

epub→pdf | which 在线转换??好用!!

1、PDF派&#xff08;free&quick) pdf转word_pdf转换成excel_pdf转换成ppt _纬来PDF转换器 评价&#xff1a;目前使用免费&#xff0c;转化的时候有进度条提示&#xff0c;总的来说比较快&#xff0c;50mb的文件在40秒内可以转换完成&#xff0c;推荐 2、pdfconvert(free…

MySQL数据高效集成到金蝶云星空的技术分享

MySQL数据集成到金蝶云星空的技术案例分享&#xff1a;SR新建调拨单内部供应商-深圳天一 在企业信息化系统中&#xff0c;数据的高效流动和准确对接是实现业务流程自动化的关键。本文将聚焦于一个具体的系统对接集成案例——将MySQL中的数据集成到金蝶云星空&#xff0c;以支持…

Linux系统 - 基本概念

介绍一些Linux系统的基本概念 1 操作系统的核心—内核 “操作系统”通常包含两种不同含义。 1&#xff0e;指完整的软件包&#xff0c;这包括用来管理计算机资源的核心层软件&#xff0c;以及附带的所有标准软件工具&#xff0c;诸如命令行解释器、图形用户界面、文件操作工具…

PDF电子发票数据提取至Excel

声明&#xff1a;本软件是吾爱大佬th4c3y原创&#xff0c;本人只是搬运工&#xff01; 发票识别更新记录 【2025-3-14】更新 v2.0 在字段设置中新增自定义字段&#xff08;仅在 PDF 正则式接口下生效&#xff09;&#xff0c;支持自定义正则表达式或固定字符。 自定义字段会…

【身份证识别表格】把大量手机拍摄的身份证信息转换成EXCEL表格的数据,拍的身份证照片转成excel表格保存,基于WPF和腾讯OCR的实现方案

基于WPF和腾讯OCR的身份证照片转Excel方案 应用场景 ​​企业人事管理​​&#xff1a;新员工入职时批量录入数百份身份证信息&#xff0c;传统手动录入易出错且耗时。通过OCR自动提取姓名、身份证号等字段&#xff0c;生成结构化Excel表格&#xff0c;效率提升10倍以上。 ​​…

FPGA高速接口 mipi lvds cameralink hdml 千兆网 sdi

mipi: https://blog.csdn.net/SDJ_success/article/details/146541776 cameralink CameraLink协议 CameraLink协议是一种专门针对机器视觉应用领域的串行通信协议&#xff0c;它使用低压差分信号(LVDS)进行数据的传输和通信。CameraLink标准是在ChannelLink标准的基础上多加了…

Linux路径解析指南:逻辑路径 vs 实际路径详解

在 Linux 系统中&#xff0c;逻辑路径&#xff08;Logical Path&#xff09;和 实际路径&#xff08;Physical Path&#xff09;是两个不同的概念&#xff0c;主要区别在于它们如何解析文件或目录的位置。以下是详细解释&#xff1a; 目录 1. 逻辑路径&#xff08;Logical Path…

Azure 公有云基础架构与核心服务:从基础到实践指南

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 Azure 的基础架构由多个核心组件构成&#xff0c;理解这些概念是掌握其技术框架的第一步&#xff1a; 地理区域&#xff08;Geographic R…

【运维_日常报错解决方案_docker系列】一、docker系统不起来

今天忽然想起来哎&#xff0c;还有一台”尘封“着的服务器&#xff0c;好久没用了&#xff0c;就随便打开登了登&#xff0c;然后想看一下服务器上面还有正在跑着的容器服务吗&#xff0c;然后使用docker ps 发现报错了。 然后重启也是下面这个状态。 查看docker状态&#xf…

C# 数组与字符串:全面解析与应用实践

在C#编程语言中&#xff0c;数组和字符串是两种最基础也是最重要的数据类型。无论是简单的控制台应用程序&#xff0c;还是复杂的企业级系统&#xff0c;数组和字符串都扮演着不可或缺的角色。本文将全面深入地探讨C#中数组和字符串的特性、使用方法、性能考量以及实际应用场景…