Mic*_*l M 5 python styles xlwt
我需要在我通过我的程序创建的xls文件中对某些单元格和行进行样式化,但是我遇到了一些问题,可能存在关于xlwt easyxf内容如何工作的误解.
首先,如果我写入没有值的单元格而只是样式,那么内部的值是否会被删除?
其次,我正在尝试使用单元格的样式和值写入单元格,但我一直收到错误:
"TypeError: 'XFStyle' object is not callable". -Solved
Run Code Online (Sandbox Code Playgroud)
现在的问题是样式没有实现.写入单元格并将其输出到xls文件后,根本没有颜色,bg,大小,字体更改.
我尝试使用Google搜索并关注其他人的示例,但无论出于何种原因,我的代码都无效.这里是:
def stylize_spreadsheet_accordingly(iFile, workbook, row_grey, row_underline, row_font_size, cells_yellow):
#this time iFile is the file you're overwriting
#styling stuff
print "styling the document..."
new_sheet.col(0).width = 256 * 18
new_sheet.col(1).width = 256 * 69.43
new_sheet.col(2).width = 256 * 9
new_sheet.col(3).width = 256 * 20.71
new_sheet.col(4).width = 256 * 8.43
font_size_style = xlwt.easyxf('font: name Calibri, bold on, height 280;')
font_underline_style = xlwt.easyxf('font: underline on;')
fill_grey_style = xlwt.easyxf('pattern: back_color gray25;')
fill_yellow_style = xlwt.easyxf('pattern: back_color yellow;')
iBook = open_workbook(iFile)
iSheet = iBook.sheet_by_index(0)
for row_index in range(iSheet.nrows):
if row_index in row_grey:
for col_index in range(iSheet.ncols):
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
if row_index in row_underline:
for col_index in range(iSheet.ncols):
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_underline_style)
if row_index in row_font_size:
for col_index in range(iSheet.ncols):
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_size_style)
for each in cells_yellow:
new_sheet.write(each[0], each[1], iSheet.cell(each[0],each[1]).value, fill_yellow_style)
return workbook
Run Code Online (Sandbox Code Playgroud)
new_sheet是一个全局变量,我在另一个函数中创建,表示我添加到我的xlwt工作簿的工作表.我传入的工作簿是应该包含它的文件new_sheet.我可能会让它变得复杂或不道德,但它确实有效.
PS如果我有不同的方式可以做到这一点或者以不同的方式将某些单元格改为全彩色,请告诉我.再一次,谢谢.
谢谢,我固定的代码什么你们说,和类型错误走开了,但完成后,没有我创建和使用的样式选项,穿了过去.xls文件仍然是默认格式.怎么会这样?
你得到的是'XFStyle' object is not callable因为你像函数一样调用它而不是仅仅将它传递给sheet.write例如
代替
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style())
Run Code Online (Sandbox Code Playgroud)
使用
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
Run Code Online (Sandbox Code Playgroud)