如何解决问题:保存时出现“预期<class'openpyxl.styles.fills.Fill'>”

时间:2019-09-02 14:54:29

标签: python-3.x openpyxl

每次保存文件都会收到错误消息:

 raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'openpyxl.styles.fills.Fill'>

然后无法再访问该保存的文件(当我手动打开它时)

open and load succesfully the file with openpyxl library:
book = openpyxl.load_workbook(r'C:\Users\shoshana\PycharmProjects\pandas\doh_golmi.xlsx')

保存方式:

book.save(r'C:\Users\shoshana\PycharmProjects\pandas\doh_golmi_2.xlsx')

然后

import openpyxl
......
openpyxl.load_workbook(r'C:\Users\shoshana\PycharmProjects\pandas\doh_golmi.xlsx')
....
book.save(r'C:\Users\shoshana\PycharmProjects\pandas\doh_golmi_2.xlsx')

当然希望能够保存我在文件中所做的更改,并在手动打开文件后对其进行访问。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,并且我错误地使用了cell.fill。换句话说,我正在做某种事情

import openpyxl
openpyxl.load_workbook(r'C:\some\file.xlsx')
...
yellow = openpyxl.styles.colors.Color(rgb='FFFF00')
cell.fill = yellow 
...
book.save(r'C:\some\file.xlsx')

以上内容不正确,您必须使用PatternFill(请参见下文)

import openpyxl
openpyxl.load_workbook(r'C:\some\file.xlsx')
...
yellow = openpyxl.styles.colors.Color(rgb='FFFF00')
filling = openpyxl.styles.fills.PatternFill(patternType='solid', fgColor=color)
cell.fill = filling
...
book.save(r'C:\some\file.xlsx')