Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7167949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:40:49+00:00 2026-05-28T14:40:49+00:00

I’m writing excel sheets with xlwt, and it’s working great except for one little

  • 0

I’m writing excel sheets with xlwt, and it’s working great except for one little thing. I would like to display the number zero as a dash (-), the way that it does with accounting. However, I don’t want it to show the dollar sign, $, before everything, and I don’t want it to have commas at the thousands.

I thought this would be a fairly easy problem to solve, I just go and set the format in excel to what I want, then view “Format -> Custom Format” to get the exact format string. Doing so gives me _(* (#,##0);_(* "-"_);_(@_) to use (this doesn’t deal with the commas, but does display zeros as dashes and removes the $).
Unfortunately, when I use that string in xlwt, it seems to reset it to the accounting default: _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_). I think this is a bug in xlwt, but does anyone know if there’s a way around this to let me do what I want to do?

Edit:
Don’t know why it works, but my problem seems to be solved by removing the commas from the string. That is changing:

‘_(* #,##0_);_(* (#,##0);_(* “-“);(@)’
to
‘
(* ###0_);_(* (###0);_(* “-“);(@_)’

I believe this works because xlwt doesn’t see it as a custom format when you just remove the $ and instead writes as the accounting default, but when you remove the $ and the commas it kicks it into the custom format method, which is what I want.

In response to John, I’m using these with the tablestyle.numformat attribute. basic code would be:

style = xlwt.xlwt.XFStyle()

style.num_format_str = '_(* ###0_);_(* (###0);_(* "-"_);_(@_)'

The only thing I would ideally like to change now is make negative numbers appear like -5, rather than accounting (5), but I think I can live with how it is now. It might be nice if someone could explain how the excel format strings work though, I can see a bit of rhyme/reason in them but am mostly confused by these long string of numbers that I just cut and paste in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:40:50+00:00Added an answer on May 28, 2026 at 2:40 pm

    Here is some minimal working code. In the output spreadsheet, column A is using your original format, B is using what you first appeared to be wanting (accounting format), and column C gives you non-accounting format.

    import xlwt
    custom_fmts = (
        '_(* (#,##0);_(* "-");(@)',
        '_(#0_);(#0);"-"_)', # positive;negative;zero
        '#0;-#0;"-"', # positive;negative;zero
        )
    xfs = [xlwt.easyxf('', fmt) for fmt in custom_fmts]
    wb = xlwt.Workbook()
    ws = wb.add_sheet('x')
    for colx, xf in enumerate(xfs):
        for rowx, value in enumerate((1234567, 0, -1234567)):
            ws.write(rowx, colx, value, xf)
    
    wb.save('demo_custom_format.xls') 
    

    You may like to read the tutorial that you can access via this link.

    It might be nice if someone could explain how the excel format strings
    work

    No good telling you to RTFM because there hasn’t been a manual since about Excel 5.0 AFAIK but you can always HTF1K (hit the F1 key) then type custom formats into the search box and then FYN (follow your nose).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.