[Python學習筆記] 使用xlwings 插入註解 (forked 版本)

时间:2022-05-22 12:05:51

到今天為止 xlwings 還沒有插入註解的功能

去原始開發者的 Github Pull Requests

他說之前有人有建議要加入這個功能 但他還沒更新~

如果需要使用 Python 來插入註解的話 可以用我改好 forked 出來的 xlwings

使用方式如下

from xlwings import Workbook, Sheet, Range, Chart
wb = Workbook(r'Excel 檔案路徑')
wb = Workbook.caller()
#讀取註解
x= Range('A1').comment
print x
#使用xlwings插入註解
Range('B5').comment=u'中文註解'

對原始的 xlwings 我做了以下更改

在 xlwings/_xlwindows.py 檔案389行後我加入了以下

def get_comment(xl_range):
try:
xl_range.Comment.Text() except:
return "None" def set_comment(xl_range, value): xl_range.ClearComments()
xl_range.AddComment()
xl_range.Comment.Visible = True
value=(value).decode('utf-8')
xl_range.Comment.Text(value)

在檔案 xlwings/main.py 877行之後我加入了以下

@property
def comment(self):
return xlplatform.get_comment(self.xl_range) @comment.setter
def comment(self, value):
xlplatform.set_comment(self.xl_range, value)

會這樣更改xlwings是參考 我前篇文章 [Python學習筆記] 利用 Python在Excel 插入註解