我使用xlrd创建了一个脚本,从多个excel文件中的多个单元格中提取多个数据,并使用xlwt将这些数据写入新的excel文件 . 在新的excel文件中,我添加了两个额外的行,其中包含计算平均值和ttest的公式 . 现在我'm trying to add a script that will search through the ttest line and all the values that are under 0.05 to be colored in red. On stackoverflow I found some help but I still receive an error. (For the color, I'米使用这个来源:https://pypi.python.org/pypi/xlwt)
请你帮助我好吗 ?
谢谢 !
from xlwt import *
style = xlwt.easyxf('font: colour red, bold on')
wb=xlwt.Workbook()
wbs=wb.add_sheet("sheet_to_write")
w=xlrd.open_workbook("file_to_read.xlsx")
ws=w.sheet_by_name("sheet_to_read")
c=ws.cell(2,6).value
wbs.write(46,1,c)
... #same as the last two lines, extracting different cells from the sheet_to_red and writing them in the sheet_to_write
wbs.write(61,1,Formula("TTEST($B3:$B18, $B19:$B57, 2, 2)"))
旧代码:
for p in range(61):
p.append(str(sheet.cell(row,col).value))
if p.value < 0.05:
cell.color =='22'
代码2:
for row in range(61):
for col in range(wbs.nrows):
cell=ws.cell(row,col)
try:
if float(cell.value) < 0.05:
cell.color =='22'
except ValueError: pass
AttributeError: 'Cell' object has no attribute 'color'
代码3:
for row in range(61):
for col in range(wbs.nrows):
search=wbs.cell(row,col)
try:
if float(search.value) < 0.05:
wbs.write(row, col, search.value, style)
except ValueError: pass
ERROR:
AttributeError: 'Worksheet' object has no attribute 'cell',
我的结论:这个方法不起作用,因为xlwt没有属性单元格或nrows,这些属性特定于xlrd . 因此,唯一可行的方法是创建另一个将使用xlrd的文件,搜索特定值,并将其写入新文件 . 感谢Pyrce和tmrlvi的帮助!
1 回答
当你只想要一个赋值时,你试图将一个字符串附加到一个整数 . 我猜你打算这样做:
另外正如您所看到的,xlwt中的颜色分配与您预期的略有不同 . 您可能还需要遍历所有单元格并将它们复制到输出工作表,或共享读取的相同工作表,以使其完全符合您的要求 .