I want get comment from an excel sheet protected with python using xlwings, but it returns None

62 views Asked by At

I want get comment from an excel sheet protected with python using xlwings, but it returns None, even though it has a comment

import pandas as pd
import xlwings as xw


PATH = 'C:/Users/Hendo/Pictures/Sample.xlsx'
psw = '1234'
wb = xw.Book(PATH, password= psw)
sheet = wb.sheets['Sheet1']
print(sheet.range('H15').note)
print(sheet.range('H15').value)

It prints:

None
AAAA

I hope to return the cells comment(s)

1

There are 1 answers

3
moken On

Use the pywin32 interface and try changing your code to

import xlwings as xw


PATH = 'C:/Users/Hendo/Pictures/Sample.xlsx'
psw = '1234'

wb = xw.Book(PATH, password=psw)
sheet = wb.sheets['Sheet1']

print(sheet.range('H15').api.Comment.Text())
print(sheet.range('H15').value)

Other method
This will Print all comments on all Sheets in the workbook.

import xlwings as xw


excel_file = 'C:/Users/Hendo/Pictures/Sample.xlsx'
with xw.App(visible=False) as app:
    wb = xw.Book(excel_file)

    for sheet in wb.sheets:
        print(f"----Sheet '{sheet.name}'------------")
        for cmt in sheet.api.Comments:
            print(f"Comment Co-ordinate: {cmt.Parent.Address.replace('$', '')}")
            print(f"Comment Author: {cmt.Author}")
            print(f"Comment Text: {sheet.range(cmt.Parent.Address).api.Comment.Text()}")
            print("----------------")