openpyxl 带格式复制单元格,有一些bug,暂时搞不明白

本文介绍了一种使用Python的openpyxl库来复制Excel文件中所有样式(包括合并单元格、行高、列宽等)的方法。该方法通过逐行逐列读取源Excel文件,并将读取到的所有样式应用到目标Excel文件中。
import openpyxl
from openpyxl.styles import Font, colors, Alignment
from openpyxl.utils import get_column_letter, column_index_from_string
from copy import copy # 浅拷贝

def info(obj,flag=True):
    for i in dir(obj):
        if flag:
            if not i.startswith('_'):
                print(i)
        else:
            print(i)
    exit()

source_path = r'c:/users/administrator/desktop/bill.xlsx'
source_workbook = openpyxl.load_workbook(source_path)
source_sheet = source_workbook['空表']

target_workbook = openpyxl.Workbook()
target_sheet = target_workbook.active

nrows = 38 #source_sheet.max_row
ncols = 24 #source_sheet.max_column

for it in source_sheet.merged_cells:
    target_sheet.merge_cells(it.coord)

for r in range(1,nrows+1):
    target_sheet.row_dimensions[r].height = source_sheet.row_dimensions[r].height
    for c in range(1,ncols+1):
        if r == 1:
            column_letter = get_column_letter(c)
            column_width = source_sheet.column_dimensions[column_letter].width
            target_sheet.column_dimensions[column_letter].width = column_width
            print(column_letter+str(r),column_width)
            # 这个获取宽度的方法,返回的值不准确,
            
        source_cell = source_sheet.cell(row = r, column = c)
        target_cell = target_sheet.cell(row = r, column = c)
        
        target_sheet.cell(row = r, column = c, value = source_cell.value)
        target_cell.fill = copy(source_cell.fill)
        
        if source_cell.has_style:
            target_cell._style = copy(source_cell._style)
            target_cell.font = copy(source_cell.font)
            target_cell.border = copy(source_cell.border)
            target_cell.fill = copy(source_cell.fill)
            target_cell.number_format = copy(source_cell.number_format)
            target_cell.protection = copy(source_cell.protection)
            target_cell.alignment = copy(source_cell.alignment)
            
        if source_cell.hyperlink:
            target_cell.hyperlink = copy(source_cell.hyperlink)
            
        if source_cell.comment:
            target_cell.comment = copy(source_cell.comment)

target_workbook.save(r'c:/users/administrator/desktop/demo3.xlsx')
        

 

复制有合并单元格的行,你需要使用openpyxl的`copy_cells()`方法。首先,你需要选择要复制的行和列: ```python from openpyxl import load_workbook # 打开工作簿并选择工作表 workbook = load_workbook(filename='example.xlsx') worksheet = workbook['Sheet1'] # 选择要复制的行和列 source_row = worksheet[2] target_row = worksheet[3] ``` 然后,你需要循环遍历源行中的单元格复制它们到目标行中。如果源单元格是一个合并单元格,你需要使用`merge_cells()`方法将它们合并到目标单元格中: ```python # 循环遍历源行中的单元格复制它们到目标行中 for source_cell, target_cell in zip(source_row, target_row): # 设置目标单元格的值 target_cell.value = source_cell.value # 如果源单元格是一个合并单元格,将它们合并到目标单元格中 if source_cell.coordinate in worksheet.merged_cells: for merged_cell in worksheet.merged_cells.ranges: if source_cell.coordinate in merged_cell: # 计算合并单元格的起始和结束坐标 start_col, start_row, end_col, end_row = merged_cell.bounds # 将合并单元格复制到目标单元格中 worksheet.merge_cells(start_row=target_cell.row, start_column=start_col, end_row=target_cell.row + end_row - start_row, end_column=end_col) ``` 最后,你需要将更改保存回工作簿: ```python # 保存更改 workbook.save('example.xlsx') ``` 这就是如何使用openpyxl复制有合并单元格的行的方法。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值