gpt4 book ai didi

Python xlwt : using easyxf to stylize cells when writing bug

转载 作者:行者123 更新时间:2023-12-04 21:20:51 25 4
gpt4 key购买 nike

我需要对通过我的程序创建的 xls 文件中的某些单元格和行进行样式化,但我遇到了一些问题,可能对 xlwt easyxf 内容的工作方式存在误解。

首先,如果我写入没有值而只有样式的单元格,里面的值会被删除吗?

其次,我尝试使用单元格的样式和值写入单元格,但我不断收到错误消息:

"TypeError: 'XFStyle' object is not callable". -Solved

现在的问题是样式没有得到实现。写入单元格并将其输出到 xls 文件后,颜色、背景、大小、字体完全没有变化。

我尝试在谷歌上搜索并遵循其他人的示例,但无论出于何种原因,我的代码都不起作用。这里是:
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
new_sheet是我在另一个函数中创建的全局变量,它表示我添加到 xlwt 工作簿中的工作表。我传入的工作簿是应该包含 new_sheet 的文件。 .我可能过于复杂化或不道德地做这件事,但它确实有效。

附言如果有不同的方法可以做到这一点或以不同的方式将某些单元格更改为全色,请告诉我。再次感谢。

谢谢,我将代码修复为你们所说的,并且 TypeError 消失了,但是在它完成后,我创建和使用的任何样式选项都没有通过。 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())


new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)

关于Python xlwt : using easyxf to stylize cells when writing bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11995141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com