Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Adjusting Column Length and Row Height #34

Closed
jmcgrath207 opened this issue Jan 5, 2018 · 6 comments
Closed

Auto Adjusting Column Length and Row Height #34

jmcgrath207 opened this issue Jan 5, 2018 · 6 comments
Assignees
Milestone

Comments

@jmcgrath207
Copy link

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')
sf.to_excel(ew)
ew.save()

Here are the versions I am using

Python: 3.5.2
StyleFrame: 1.3.1
Pandas 0.20.3
openpyxl: 2.2.5

@DeepSpace2
Copy link
Owner

DeepSpace2 commented Jan 5, 2018

What is the current output you're getting vs what you have expected to get?

You have several options:

  • Use sf.set_column_width.
  • Pass shrink_to_fit and/or wrap_text with False for Styler:
    sf = StyleFrame(df, Styler(shrink_to_fit=False, wrap_text=False))

@jmcgrath207
Copy link
Author

Thanks for getting back to me so quickly

Running this gives me

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

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

sf =  StyleFrame(df, Styler(shrink_to_fit=False, wrap_text=False))
ew = StyleFrame.ExcelWriter('pandas_simple.xlsx')
sf.to_excel(ew)
ew.save()

image

I am wanting something like
image

I am displaying the excel sheet with libre calc on Linux mint 18.2 if that helps

@DeepSpace2
Copy link
Owner

DeepSpace2 commented 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'],
                   'ZZ': ['ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', 'ZZ']})
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')
sf.to_excel(ew).save()

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

@DeepSpace2 DeepSpace2 self-assigned this Feb 21, 2018
@DeepSpace2 DeepSpace2 added this to the 1.4 milestone Feb 21, 2018
@DeepSpace2
Copy link
Owner

Solved in version 1.4. See information about best_fit argument of to_excel method and the note at the bottom: http://styleframe.readthedocs.io/en/latest/api_documentation.html#to-excel

@jnw1
Copy link

jnw1 commented Oct 16, 2022

Solved in version 1.4. See information about best_fit argument of to_excel method and the note at the bottom: http://styleframe.readthedocs.io/en/latest/api_documentation.html#to-excel

I found it's not working when a cell include multiple lines, always one line's height even if I use both "wrap_text=True" and best_fit=columns. And width is too large since it compress all words in a line.
image

@DeepSpace2
Copy link
Owner

@jnw1 I'm not sure I follow, can you please explain again? Also, see #87.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants