• 使用Python复制和粘贴excel中的数据(保持源格式)


    import openpyxl as xl
    from copy import copy
    style_attrs = ["alignment", "border", "fill", "font", "number_format", "protection"]
    def cells(worksheet):
        """Return a generator for the sequence of cells in the worksheet"""
        for row in worksheet:
            for cell in row:
                yield cell
    def copy_attrs(src, dst, attrs=style_attrs):
        """Copy attributes from src to dst. Attributes are shallow-copied to avoid
        TypeError: unhashable type: 'StyleProxy'"""
        for name in attrs:
            setattr(dst, name, copy(getattr(src, name)))
    def copy_column_attrs(worksheet_src, worksheet_dst, attrs=style_attrs + ["width"]):
        """Copy ColumnDimension properties from worksheet_src to worksheet_dst.
        Only properties listed in attrs will be copied."""
        for column, dimensions in worksheet_src.column_dimensions.items():
            copy_attrs(
                src=dimensions,
                dst=worksheet_dst.column_dimensions[column],
                attrs=attrs,
            )
    def copy_row_attrs(worksheet_src, worksheet_dst, attrs=style_attrs + ["height"]):
        """Copy RowDimension properties from worksheet_src to worksheet_dst.
        Only properties listed in attrs will be copied."""
        for row, dimensions in worksheet_src.row_dimensions.items():
            copy_attrs(
                src=dimensions,
                dst=worksheet_dst.row_dimensions[row],
                attrs=style_attrs + ["height"],
            )
    def copy_cells(worksheet_src, worksheet_dst, attrs=style_attrs):
        """Copy cells from worksheet_src to worksheet_dst. If cells are styled
        then also copy the attributes listed in attrs."""
        for cell in cells(worksheet_src):
            cell_dst = worksheet_dst.cell(row=cell.row, column=cell.column)
            if cell.has_style:
                copy_attrs(cell, cell_dst, attrs=attrs)
            cell_dst.value = cell.value
    def delete_worksheet_cells(worksheet):
        worksheet.delete_cols(1, worksheet.max_column + 1)
        worksheet.delete_rows(1, worksheet.max_row + 1)
    wb_src = xl.load_workbook("a.xlsx")
    ws_src = wb_src.active
    wb_dst = xl.load_workbook("b.xlsx")
    ws_dst = wb_dst.active
    delete_worksheet_cells(ws_dst)
    copy_column_attrs(ws_src, ws_dst)
    copy_row_attrs(ws_src, ws_dst)
    copy_cells(ws_src, ws_dst)
    wb_dst.save("b.xlsx")
  • 相关阅读:
    待思考问题---数据库的分层模型
    iOS的常用类库
    java spring是元编程框架---使用的机制是注解+配置
    Java Virtual Machine (JVM), Difference JDK, JRE & JVM – Core Java
    Spring注解的使用和组件扫描
    Spring Boot实战:拦截器与过滤器
    问题、存在与认知---问题产生的原因是对存在的不完全或错误认知
    元编程的分层模型
    元编程的本质---高级语言的表达通过解释生成低级语言的表达(代码)
    aop分层模型——aop是元编程的一种
  • 原文地址:https://www.cnblogs.com/wangcongxing/p/15679511.html
Copyright © 2020-2023  润新知