# How to print values of an xlsxwriter format item?

**URL:** <https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585>\
**Category:** Python Help\
**Created:** [April 9, 2024, 2:06pm UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585 "2024-04-09T14:06:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![c-rob](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/c-rob/32/17261_2.png) [@c-rob](https://discuss.python.org/u/c-rob)\
**Post date:** [April 9, 2024, 2:06pm UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585/1 "2024-04-09T14:06:50Z")

</div>

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

```python
    workbook = writer.book
    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():

```python
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)
    else: 
        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:

```python
(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!

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [April 9, 2024, 2:57pm UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585/2 "2024-04-09T14:57:54Z")

</div>

> [@c-rob](#):
>
> How do I print this hdrbigfmt so I can see the values nicely?

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.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [April 9, 2024, 8:52pm UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585/3 "2024-04-09T20:52:26Z")

</div>

> [@c-rob](#):
>
> A format item is created like this so I thought it was just a dictionary:
> 
> ``

Of course it doesn’t work that way. The library is already violating standards by `return`ing 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**.

> [@c-rob](#):
>
> ```python
> procname = str(inspect.stack()[0][3]) + ":"
> 
> ```

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`.

> [@c-rob](#):
>
> How do I print this hdrbigfmt so I can see the values nicely?

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.

---

<div class="post-metadata">

**Author:** ![FelixLeg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/felixleg/32/18439_2.png) [@FelixLeg](https://discuss.python.org/u/FelixLeg)\
**Post date:** [April 10, 2024, 7:05am UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585/4 "2024-04-10T07:05:30Z")

</div>

(Clippy) “It looks you are writing ~~a letter~~ an Excel sheet” 😛

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:

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

```

> [@kknechtel](#):
>
> But this is an object that was not designed to be printed, because the library authors saw no value in it.

Or they were just plain lazy 😛

---

<div class="post-metadata">

**Author:** ![c-rob](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/c-rob/32/17261_2.png) [@c-rob](https://discuss.python.org/u/c-rob)\
**Post date:** [April 13, 2024, 5:34pm UTC](https://discuss.python.org/t/how-to-print-values-of-an-xlsxwriter-format-item/50585/5 "2024-04-13T17:34:02Z")

</div>

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](https://xlsxwriter.readthedocs.io/format.html)

Does anyone want to give it a try?
