如何使用win32格式化从粗体到正常的注释?

时间:2016-06-27 13:58:16

标签: python excel

我刚给excel文件中的单元格添加了注释。不知怎的,它以粗体显示,我不想要。我该怎么做才能解决它?

我的代码:

excel = win32.gencache.EnsureDispatch('Excel.Application')
file_location = 'C:\Users\A42918\Desktop\Test_folder\Abild, T1.xls'
wb = excel.Workbooks.Open(file_location)

ws = wb.Worksheets("Trelindad_utan")
ws.Range('AH4').Interior.ColorIndex = 37
ws.Range('AH4').Font.Bold = True
comment = 'E14678: Vinkel i nollpunkt. Sätts lika med vinkeln på I-bus i balansnätet (om denna avviker mycket från noll grader).'.decode('utf-8')
ws.Range('AH4').AddComment(comment)
cmt = ws.Range('AH4').AddComment(comment)

cmt.Shape.TextFrame.Characters.Font.Bold = False 

wb.SaveAs(Filename = 'C:\Users\A42918\Desktop\color_resultat.xls')

excel.Application.Quit()

这给了我错误消息:cmt.Shape.TextFrame.Characters.Font.Bold = False AttributeError:'功能'对象没有属性'字体'

这是什么意思?

2 个答案:

答案 0 :(得分:0)

为此,您需要处理Comment对象。表示单元格“AH4”的Range对象是独立格式化的;更改单元格上的字体格式(例如粗体)不会对评论产生任何影响。

这应解决它:

excel = win32.gencache.EnsureDispatch('Excel.Application')
file_location = 'C:\Users\A42918\Desktop\Test_folder\Abild, T1.xls'
wb = excel.Workbooks.Open(file_location)

ws = wb.Worksheets("Trelindad_utan")
ws.Range('AH4').Interior.ColorIndex = 37
ws.Range('AH4').Font.Bold = True
comment = 'E14678: Vinkel i nollpunkt. Sätts lika med vinkeln på I-bus i balansnätet (om denna avviker mycket från noll grader).'.decode('utf-8')
# remove any previous comments just in case
ws.Range('AH4').ClearComments 
# Add the comment, and its text:
cmt = ws.Range('AH4').AddComment(comment)
# Ensure the comment is not bolded font
cmt.Shape.TextFrame.Characters().Font.Bold = False

答案 1 :(得分:0)

这解决了我的问题!

excel = win32.gencache.EnsureDispatch('Excel.Application')
file_location = 'C:\Users\A42918\Desktop\Test_folder\Abild, T1.xls'
wb = excel.Workbooks.Open(file_location)

ws = wb.Worksheets("Trelindad_utan")
ws.Range('AH4').Interior.ColorIndex = 37 
ws.Range('AH4').Font.Bold = True

ws.Range('AH4').ClearComments 
comment = 'E14678: Vinkel i nollpunkt. Sätts lika med vinkeln på I-bus i balansnätet (om denna avviker mycket från noll grader).'.decode('utf-8')

cmt = ws.Range('AH4').AddComment(comment)

cmt.Shape.TextFrame.Characters().Font.Bold = False 

wb.SaveAs(Filename = 'C:\Users\A42918\Desktop\color_resultat.xls')

excel.Application.Quit()