![](/img/trans.png)
[英]Change font format with xlwings on Mac and get an AttributeError
[英]AttributeError: when change font colour in xlwings python
如果值行與 xlwings 中的下一個單元格相同,我想更改字體顏色。我嘗試使用“worksheet.range(changeFontCell).api.font.Color = rgb_to_int((176,176,176))”來更改字體。
但我收到錯誤消息“ AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object Library.Range instance at 0x2163906949584>' object has no attribute 'font' ”下面是我的源代碼。 我該如何解決該錯誤
lastRow = worksheet.range('A' + str(worksheet.cells.last_cell.row)).end('up').row + 1
startNewRow = 'A' + str(lastRow)
worksheet.range(startNewRow).value = finalResult
finalLastRow = worksheet.range('A' + str(worksheet.cells.last_cell.row)).end('up').row
for i in list(range(lastRow, finalLastRow)):
if worksheet.range('A' + str(i)).value == worksheet.range('A' + str(i+1)).value:
changeFontCellColour = 'A' + str(i+1)
worksheet.range(changeFontCellColour ).api.font.Color = rgb_to_int((176,176,176))
else:
continue
任何建議都有幫助! 謝謝
現在我用以下來源解決了我的問題。
lastRow = worksheet.range('A' + str(worksheet.cells.last_cell.row)).end('up').row + 1
startNewRow = 'A' + str(lastRow)
worksheet.range(startNewRow).value = finalResult
finalLastRow = worksheet.range('A' + str(worksheet.cells.last_cell.row)).end('up').row
for i in list(range(lastRow, finalLastRow)):
if worksheet.range('A' + str(i)).value == worksheet.range('A' + str(i+1)).value:
changeFontCellColour = 'A' + str(i+1)
**myRange = worksheet.range(changeFontCellColour)**
myRange.api.Font.Color = rgb_to_int((160,160,160))
continue
else:
continue
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.