Search code examples
pythonpython-3.xpywin32

SetRange method returns NoneObject in pywin32


I'm trying to make some part of text in table cell in word document bold with python. But I can't figure out how to do it.

I've already tried using range method with parameters, but it returns string, instead of range object. Now I'm trying to call SetRange method of Range object in Python, but it makes my Range object NoneObject.

test = table.Cell(start_row + i, 3).Range.SetRange(1, 10).Bold = True

Code crashes with

AttributeError: 'NoneType' object has no attribute 'Bold'

What am I doing wrong?


Solution

  • You're not using it properly. Check [MS.Docs]: Range object (Word):

    • SetRange - one of its methods, which doesn't return anything (translated to Python - returns None)
    • Bold - one of its properties

    So, you shouldn't chain the 2. Modify your code to:

    range_obj = table.Cell(start_row + i, 3).Range
    range_obj.SetRange(1, 10)
    range_obj.Bold = True