'Add notes using openpyxl

everyone. I'm looking for a way to make notes in excel worksheet, using python. Found a way to add comments, but I need notes like on screenshot. Is there an easy way to add them using openpyxl or any other lib? screenshot of a note



Solution 1:[1]

I've been trying to accomplish the same thing. Apparently that "note" is the same as data validation as described in the docs.

So what you do is:

from openpyxl import load_workbook
from openpyxl.worksheet.datavalidation import DataValidation

wb = load_workbook('my_sheets.xlsx')

# Create 'note'
dv = DataValidation()
dv.errorTitle = 'Your note title'
dv.error = 'Your note body'

# Add 'note' to A1 in the active sheet
dv.add(wb.active['A1'])

# This is required also, or you won't see the note
wb.active.add_data_validation(dv)

wb.save('my_sheet_with_note.xlsx')

It also mentions prompts, which is something you can look into:

# Optionally set a custom prompt message
dv.promptTitle = 'List Selection'
dv.prompt = 'Please select from the list'

Edit: I updated the answer with a part of the code that solved my problem at the time:

def add_note(cell, prompt_title='', prompt=''):
    p = {
        'promptTitle': prompt_title[:32],
        'prompt': prompt[:255],
    }
    dv = DataValidation(**p)
    dv.add(cell)
    cell.parent.add_data_validation(dv)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1