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.

Worksheet.set_column() sets the column width only once

See original GitHub issue

Inside the method:

    out = StringIO.StringIO()
    workbook = xlsxwriter.workbook.Workbook(out)
    worksheet = workbook.add_worksheet()

    worksheet.set_column('A:A', 1)
    worksheet.set_column('A:A', 100)

    workbook.close()
    out.seek(0)
    return out

Expected outcome:

snapshot2

Actual outcome:

snapshot3

I don’t think it’s a LibreOffice issue this time, because the width is, in fact, set - but only once.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Dec 18, 2013

By the way, what’ll happen if try to do this vise versa, first assigning default styles to each column individually - and then applying the custom styles to a range of columns?

The last range will show up in Excel. The first in LibreOffice.

Instead you should do something like the following, which will work without a bug fix in LibreOffice/Excel:

import xlsxwriter

workbook = xlsxwriter.workbook.Workbook('col_width.xlsx')
worksheet = workbook.add_worksheet()

colwidths = {}

# Store the defaults.
for col in range(50):
    colwidths[col] = 5

# Then override any that you want.
colwidths[4] = 15

# Then set the column widths.
for col_num, width in colwidths.items():
    worksheet.set_column(col_num, col_num, width)

workbook.close()


1reaction
jmcnamaracommented, Dec 18, 2013

So, will I not be able to, say, set the same width for the a range - and then set the width for an individual column within that range?

No.

It might work in Excel but probably not in LibreOffice.

When you split a column range in Excel you get n+1 ranges. For example this:

worksheet.set_column('A:E', 20)
worksheet.set_column('C:C', 30)

In Excel would be:

worksheet.set_column('A:B', 20)
worksheet.set_column('C:C', 30)
worksheet.set_column('D:E', 20)

If you have a small number of columns (<1000) then I would recommend just setting each one individually. Like this:

import xlsxwriter

workbook = xlsxwriter.workbook.Workbook('col_width.xlsx')
worksheet = workbook.add_worksheet()

for col in range(50):
    worksheet.set_column(col, col, 20)

# Then override any that you want.
worksheet.set_column(4, 4, 50)

workbook.close()

Note, this won’t work in LibreOffice until after the bug fix for this issue but it currently works with Excel.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Worksheet Class — XlsxWriter Documentation
The write_row() method can be used to write a list of data in one go. This is useful for ... worksheet.set_column(1, 1, 30)...
Read more >
Simulate autofit column in xslxwriter - python - Stack Overflow
As a general rule, you want the width of the columns a bit larger than the size of the longest string in the...
Read more >
How to change and AutoFit column width in Excel - Ablebits
Another way to autofit columns in Excel is by using the ribbon: select one or more columns, go to the Home tab >...
Read more >
Change the column width and row height - Microsoft Support
Individual rows and columns can only have one setting. For example, a single column ... Change the default width for all columns on...
Read more >
How to: Specify Row Height or Column Width - C# & VB.NET
Set Column Width. This example demonstrates how to control the column width in a worksheet. #Set the Width for Individual Columns. To specify ......
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