首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用python复制和粘贴excel中的数据(保持源格式)

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

Stack Overflow用户
提问于 2021-07-19 20:20:56
回答 1查看 4.7K关注 0票数 2

我想用源格式(单元格模式、单元格边框、字体样式、字体颜色、字体大小、页眉和页脚)将数据从工作簿中的一个工作表复制到另一个工作表中。这是我使用的代码,它做的工作,但它未能保持源格式。如果你们有任何可能在这个话题上有帮助的东西,那就太好了。

源代码:

代码语言:javascript
运行
复制
import openpyxl as xl;
from copy import copy
wb1 = xl.load_workbook('C:\\Users\\high.xlsx')
ws1 = wb1.active
filename = 'C:\\Users\\3.0.xlsx'
wb2 = xl.load_workbook(filename)
ws2 = wb2['Session']


mr = ws1.max_row
mc = ws1.max_column
mr2 = ws2.max_row
mc2 = ws2.max_column

ws2.delete_cols(1, mc2+1)
ws2.delete_rows(1, mr2+1)

for i in range (1, mr + 1):
    for j in range (1, mc + 1):

        c = ws1.cell(row = i, column = j)


        ws2.cell(row = i, column = j).value = c.value
wb2.save(str(filename))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-19 22:06:28

单元格对象上有许多属性,您可以在其中复制具有样式信息的属性。尝试直接复制它们将失败,但使用复制模块将有效。

样式信息也可以附加到行和列-它们似乎是附加到工作表上的维度对象

Update: Oops,看起来这里有已获答复 --不过我将把它留在这里,因为它包含行和列级别的样式。

代码语言:javascript
运行
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68446561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档