如何将单元格填充颜色从一个文档复制到另一个文档?
How can I copy cell fill color from one document to another?
我正在尝试打开一个文件,查看单元格是否有颜色填充,然后将坐标和填充信息复制到字典中。然后,我想遍历字典以将格式复制到不同文档中的相同坐标中。 (这可能不是完成此任务的最佳方法)
def color_collect(wb): # This collects colors from cell coordinates
color_data = OrderedDict()
for sheetcount, wksht in enumerate(wb.worksheets):
color_data[sheetcount] = {}
for row in wksht:
for cell in row:
coord = cell.coordinate
coordcolor = cell.fill.start_color.index
if coordcolor != '00000000':
color_data[sheetcount][coord] = coordcolor
return color_data
def color_write(wb, color_data): # This adds color to cell coordinates
print color_data
for idx, sheet in enumerate(wb):
print idx
for cell in color_data[idx]:
print cell
coloridx = color_data[idx][cell]
print coloridx
sheet[cell].fill.start_color.index = color_data[idx][cell]
运行 这给出了这个结果:
sheet[cell].fill.start_color.index = color_data[idx][cell]
AttributeError: can't set attribute
一些注意事项:索引颜色很重要,因为我使用索引颜色将颜色编码的数据导入到 PowerPoint 中。如果未正确应用索引,则稍后无法识别数据,因此使用普通 RGB 填充的解决方案不是很有用。
任何帮助都会有帮助
这最好由 copying styles
完成
类似于sheet[c1].fill = copy(c2.fill)
我正在尝试打开一个文件,查看单元格是否有颜色填充,然后将坐标和填充信息复制到字典中。然后,我想遍历字典以将格式复制到不同文档中的相同坐标中。 (这可能不是完成此任务的最佳方法)
def color_collect(wb): # This collects colors from cell coordinates
color_data = OrderedDict()
for sheetcount, wksht in enumerate(wb.worksheets):
color_data[sheetcount] = {}
for row in wksht:
for cell in row:
coord = cell.coordinate
coordcolor = cell.fill.start_color.index
if coordcolor != '00000000':
color_data[sheetcount][coord] = coordcolor
return color_data
def color_write(wb, color_data): # This adds color to cell coordinates
print color_data
for idx, sheet in enumerate(wb):
print idx
for cell in color_data[idx]:
print cell
coloridx = color_data[idx][cell]
print coloridx
sheet[cell].fill.start_color.index = color_data[idx][cell]
运行 这给出了这个结果:
sheet[cell].fill.start_color.index = color_data[idx][cell]
AttributeError: can't set attribute
一些注意事项:索引颜色很重要,因为我使用索引颜色将颜色编码的数据导入到 PowerPoint 中。如果未正确应用索引,则稍后无法识别数据,因此使用普通 RGB 填充的解决方案不是很有用。
任何帮助都会有帮助
这最好由 copying styles
完成类似于sheet[c1].fill = copy(c2.fill)