How to print values of an xlsxwriter format item?

A format item is created like this so I thought it was just a dictionary:

    workbook  =
    worksheet = writer.sheets['Sheet1']

    # Add a header format.
    hdrbigfmt = workbook.add_format({'bold': True, 
    'font_size':16, 'text_wrap': True,
    'valign': 'bottom', 'fg_color': 'black',
    'bg_color': 'white', 'border': 1 })    

So I made my own function printdict():

def printdict(mydict):
    r'''Print contents of a dictionary with keys.
    procname = str(inspect.stack()[0][3]) + ":"
    if isinstance(mydict,dict):
        for key, val in mydict.items():
            print(f"{key:15s} = ",val)
        print(procname,"ERROR: this is not a dict item.")

printdict() prints my own test dictionary but not the format item “hdrbigfmt”. Here’s some output I got from printdict() in a library I use in all my programs:

(Pdb) printdict(mydict)
fname           =  Jim
lname           =  Smith
age             =  32
score           =  93.2
(Pdb) printdict(hdrbigfmt)
printdict: ERROR: this is not a dict item.
(Pdb) pprint(hdrbigfmt)
<xlsxwriter.format.Format object at 0x000002537C473680>

How do I print this hdrbigfmt so I can see the values nicely? Notice that pprint() didn’t work either. It just gives me a memory address.

Thanks so much!

Try pprint(vars(hdrbigfmt))
Look at the documentation for xlsxwriter.format.Format for whichever library this is.
Failing that, look at the source code for the class.

Of course it doesn’t work that way. The library is already violating standards by returning the format object back to you from workbook.add_format, rather than returning None. But of course every function can return whatever type it likes, and accept whatever type it likes, with no correlation. If you want to understand what is returned, you should read the documentation.

As an aside, this part will not work like you want. When the printdict code is running, its own stack frame is on top of the stack, which is why you always see the name reported as printdict.

Classes have to be designed for the objects to support __str__, or else you get the default. This is a class that someone else wrote for you, so you can’t fix that directly. If you know what things are contained in the object, then you can pull them out separately to print them, with whatever formatting you like. But this is an object that was not designed to be printed, because the library authors saw no value in it.

(Clippy) “It looks you are writing a letter an Excel sheet” :stuck_out_tongue:

Just because the function workbook.add_format accepts a dict it doesn’t mean the hdrbigfmt will be a dict too. As Karl pointed out you have to check the library’s documentation to find out what parameters you can extract from this object. However there are some alternatives: in Python REPL you can create the object and then:

>>> dir(hdrbigfmt)
#prints all elements in the object
>>> help(hdrbigfmt.some_field)
#prints the doc of the "some_field" 

Or they were just plain lazy :stuck_out_tongue:

I have read the docs on the xlsxwriter and it’s format object. I could not find any class definition or find out what the format object actually is to print it. I think using pprint() just printed a memory address.

I also did a Google search and did not find many pages for this either. This is what I found.

  1. The Format Class — XlsxWriter

Does anyone want to give it a try?