xlsxwriter: How to put an internal hyperlink in a table and have content formatted as integer

StackOverflow https://stackoverflow.com/questions/21915879

  •  14-10-2022
  •  | 
  •  

Question

The snippet below works fine (including the link) - except that this is a big table with lots of links and the final sheet is loaded with warnings ("The number in this cell is formatted as text or preceded by an apostrophe"). This is true but the write method wont accept an integer in the last parameter and write_number() doesn't seem to accept hyperlinks arguments like the write method. The warning can be patched manually in excel but that is not really an option. I would prefer to have it formatted as an integer but would be satisfied if I could just stop getting all of the warnings in the resulting Excel file. I tried various format settings and tried various options including putting '{'strings_to_numbers': True}' in the workbook creation.

    stream = StringIO()
    workbook = xlsxwriter.Workbook(stream, {'strings_to_numbers': True})

    integer_format = workbook.add_format({'num_format': '#,##0'})
    linkable_sheet = workbook.add_worksheet('linkable_sheet')
    main_sheet = workbook.add_worksheet('main_sheet')       
    main_sheet.add_table(0,0,2,2)
    main_sheet.write(1, 0, 'internal:linkable_sheet!A1', integer_format, '100')

    workbook.close() 
    stream.seek(0)
    return stream.getvalue()

Thanks

Was it helpful?

Solution

XlsxWriter probably should probably accept integer/floats in situations like this. I'll look into allowing that in future versions.

In the meantime the recommended workaround in the documentation is to write the url with a dummy (or no) string and then overwrite the string part of the url with whatever type/data you wish.

Try modifying your example as follows:

...
main_sheet.write(1, 0, 'internal:linkable_sheet!A1', integer_format)
main_sheet.write(1, 0, 100,                          integer_format)
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top