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.

Auto Adjusting Column Length and Row Height

See original GitHub issue

Hi! Thanks for contributing to StyleFrame. Please make sure to mention the Python, StyleFrame, pandas and openpyxl versions you are using.

I am having an issue with the excel sheet auto adjusting the column to the widest length, below is some sample code I am having an issue with.

import pandas as pd
from StyleFrame import StyleFrame, Styler, utils

df = pd.DataFrame({'WW': ['WWWW', 'WWWWWWWWWWWWWWWWWW']})

sf = StyleFrame(df)
ew = StyleFrame.ExcelWriter('pandas_simple.xlsx')

Here are the versions I am using

Python: 3.5.2 StyleFrame: 1.3.1 Pandas 0.20.3 openpyxl: 2.2.5

Issue Analytics

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

github_iconTop GitHub Comments

DeepSpace2commented, Mar 22, 2018

Solved in version 1.4. See information about best_fit argument of to_excel method and the note at the bottom:

DeepSpace2commented, Jan 5, 2018

I’ll need to look into this.

As a temporary workaround, a possible solution would be using sf.set_column_width_dict to set each column’s width according to the longest string in it multiplied by some factor:

df = pd.DataFrame({'WW': ['WWWW', 'WWWWWWWWWWWWWWWWWW'],
sf = StyleFrame(df)
factor = 2.5
sf.set_column_width_dict({column: max(df[column].str.len()) * factor for column in df.columns})
ew = StyleFrame.ExcelWriter('pandas_simple.xlsx')

This is obviously quick and dirty, and isn’t perfect (it won’t work with all fonts and font sizes).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the column width or row height in Excel
On the Home tab, select Format > Column Width (or Column Height). Type the column width and select OK. Automatically resize all columns...
Read more >
How to AutoFit in Excel: adjust columns and rows to match ...
To AutoFit row height, select the row(s) of interest, go to the Home tab > Cells group, and click Format > AutoFit Row...
Read more >
Where is Autofit in Excel – How to Adjust Column Width and ...
Resizing the columns in the worksheet needs you to choose “Autofit Column Width” from the “Format” menu. Choose the “Autofit Row Height” option ......
Read more >
Excel AUTOFIT: Make Rows/Columns Fit the Text Automatically
AutoFit is a feature in Excel that allows you to quickly adjust the row height or column width to fit the text completely...
Read more >
Steps to size column width/row height automatically
Steps to size column width/row height automatically · Run a report. (How?) · On the Grid menu, select Options. · On the Columns...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found