如何将Excel富文本单元格复制到另一工作簿?现有代码格式丢失
解决openpyxl复制含富文本的Excel单元格时格式丢失问题
问题描述
需要将包含富文本(例如单元格内部分文字为黑色、部分为红色)的Excel单元格从一个工作簿复制到另一个工作表,但使用以下openpyxl代码实现后,目标文件中仅显示纯黑色文字,原有的红色文字格式未被保留:
from openpyxl import load_workbook from openpyxl.styles import Font, PatternFill def copy_cell_with_format(source_filename, source_sheet, source_cell, destination_filename, destination_sheet, destination_cell): # Load the source workbook source_workbook = load_workbook(filename=source_filename) source_ws = source_workbook[source_sheet] # Load the destination workbook dest_workbook = load_workbook(filename=destination_filename) dest_ws = dest_workbook[destination_sheet] # Get the source cell source_text = source_ws[source_cell].value source_font = source_ws[source_cell].font source_fill = source_ws[source_cell].fill # Copy text and format to destination cell dest_ws[destination_cell] = source_text dest_ws[destination_cell].font = Font(color=source_font.color) dest_ws[destination_cell].fill = PatternFill(start_color=source_fill.start_color.rgb, end_color=source_fill.end_color.rgb, fill_type=source_fill.fill_type) # Save the destination workbook dest_workbook.save(destination_filename) # Example usage: source_filename = "formatted_text.xlsx" source_sheet = "Sheet1" source_cell = "A1" destination_filename = "y9t_mpp(auto).xlsx" destination_sheet = "Sheet3" destination_cell = "F1" copy_cell_with_format(source_filename, source_sheet, source_cell, destination_filename, destination_sheet, destination_cell)
问题原因
原代码仅复制了单元格整体的字体样式,但富文本属于字符级格式(即单元格内不同文字段有独立格式),直接读取cell.value会将富文本转换为纯文本,丢失所有字符级格式信息;同时设置整体font属性只会给整个单元格应用单一格式,无法保留局部文字的差异化样式。
解决方案
使用openpyxl的rich_text属性来获取和设置富文本内容,该属性会完整保留单元格内每个文字段的格式信息。修正后的代码如下:
from openpyxl import load_workbook from openpyxl.styles import PatternFill def copy_cell_with_format(source_filename, source_sheet, source_cell, destination_filename, destination_sheet, destination_cell): # 加载源工作簿和工作表 source_workbook = load_workbook(filename=source_filename) source_ws = source_workbook[source_sheet] # 加载目标工作簿和工作表 dest_workbook = load_workbook(filename=destination_filename) dest_ws = dest_workbook[destination_sheet] # 获取源单元格的富文本和填充样式 source_rich_text = source_ws[source_cell].rich_text source_fill = source_ws[source_cell].fill # 复制富文本内容(含字符级格式)到目标单元格 dest_ws[destination_cell].rich_text = source_rich_text # 复制单元格填充样式 dest_ws[destination_cell].fill = PatternFill( start_color=source_fill.start_color.rgb, end_color=source_fill.end_color.rgb, fill_type=source_fill.fill_type ) # 保存目标工作簿 dest_workbook.save(destination_filename) # 示例调用 source_filename = "formatted_text.xlsx" source_sheet = "Sheet1" source_cell = "A1" destination_filename = "y9t_mpp(auto).xlsx" destination_sheet = "Sheet3" destination_cell = "F1" copy_cell_with_format(source_filename, source_sheet, source_cell, destination_filename, destination_sheet, destination_cell)
关键说明
cell.rich_text:返回一个包含文本段和对应格式的对象,完整保留单元格内的富文本结构- 直接赋值
dest_ws[destination_cell].rich_text = source_rich_text,会将源单元格的所有字符级格式(如部分文字红色、部分黑色)完整复制到目标单元格 - 单元格级的样式(如填充背景色)仍可按原方式复制,不影响富文本格式
内容的提问来源于stack exchange,提问作者Nikhil teja
相关产品推荐
相关产品推荐

