question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Corrupted output file when adding a table with "header_row: True" and no data

See original GitHub issue

I am using XlsxWriter to create tables with a header row and data underneath. Sometimes my script has no data so there is only one row which is the header row. In that case, the generated file is corrupted according to Excel.

I am using Python version 3.7.3 and XlsxWriter 1.2.8 and Excel version 2016.

Here is some code that demonstrates the problem:


#!/usr/bin/env python3
import xlsxwriter

workbook = xlsxwriter.Workbook("test.xlsx")
worksheet = workbook.add_worksheet()

data = []

worksheet.add_table(first_row=0, first_col=0,
                    last_row=len(data), last_col=1,
                    options={
                        'columns': [{'header': 'test1'}, {'header': 'test2'}],
                        'data': data,
                        'header_row': True
                    })

workbook.close()

I know that the error is on my side but IMO xlsxwriter should catch such case and trigger an error.

Actually here is how Excel handles this:

  1. Create new file
  2. A1=“test 1” and B1="test 2
  3. Select A1 and B1
  4. Insert table and check “my table as headers”
  5. Notice that the table now covers two rows (so one empty data row was added)

So xlsxwriter could try to fix it in the same spirit, for example:

if len(data)==0 and last_row==0 and options['header_row']:
    last_row=1

Which adds an empty data row and generates a valid file.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Sep 19, 2020

Fixed on master. The module now issues a warning for tables without space for data rows. The header row, or lack of one, is taken into account. Thanks for the report.

1reaction
jmcnamaracommented, May 11, 2020

The other one is slightly different I think.

Ok. Thanks. I’ll handle them separately.

Maybe both could be fixed by adding one empty row when there’s no data row.

In general XlxsWriter doesn’t try to fix input issue for the user. This will probably be fixed with a warning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache POI createTable generates corrupted file when a ...
This is an inaccuracy with XSSFTable.updateHeaders. This method gets called while the table's XML gets written. This is because the table ...
Read more >
Tidy a comma separated value (CSV) file - R
Tidies up a Comma Separated Value (CSV) file, ensuring that each row of the table in the file contains the same number of...
Read more >
jmlondon/wcUtils source: R/fixCSV.R - Rdrr.io
Function fixCSV ## ##' Tidies up a Comma Separated Value (CSV) file, ensuring that each ##' row of the table in the file...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
If file contains no header row, then you should explicitly pass header=None . ... and warn_bad_lines is True , a warning for each...
Read more >
Integration Services Error and Message Reference
Hexadecimal code Decimal Code Symbolic Name 0x8002F347 ‑2147290297 DTS_E_STOREDPROCSTASK_OVERWRITINGSPATD... 0x8020837E ‑2145352834 DTS_E_ADOSRCUNKNOWNTYPEMAPPEDTONTEXT 0x8020838C ‑2145352820 DTS_E_XMLSRCSCHEMACOLUMNNOTINEXTERNAL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found