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.

Auto column width and row height with multi-line cell values

See original GitHub issue

Hi,

I’m having an issue with the auto determined column width and row height when using StyleFrame with a pandas DataFrame that contains multi-line values.

import pandas as pd
from styleframe import StyleFrame

df = pd.DataFrame({"a": ("Line 1\nLine 2\nLine 3",)})
sf = StyleFrame(df)
sf.to_excel("test.xlsx", best_fit=list(df.columns), index=False).save()

What I expect test.xlsx to look like is:

   a   |  b
-------|------
Line 1 |Line 1
Line 2 |Line 2
Line 3 |Line 3
-------|------

What it actually looks like is:

                   a                   |                   b
---------------------------------------|--------------------------------------
                 Line 2                |                 Line 2
---------------------------------------|--------------------------------------

“Line 1” and “Line 3” are present in each cell but the row is not rendered with sufficient height for them to be visible.

This behaviour is the same regardless of any wrap_text or width properties set on the columns.

The README.md states:

The StyleFrame object will auto-adjust the columns width and the rows height but they can be changed manually

The API documentation for StyleFrame.to_excel states the formula used to determine a column’s width as:

(len(longest_value_in_column) + A_FACTOR) * P_FACTOR

…which results in column widths a little bigger than the largest combined length of all lines plus the newline characters between them in a single value.

Is there a way to instruct StyleFrame to treat multi-line value differently such that:

a. Columns that contain multi-line values are not much wider than the longest line in a value contained within them

and

b. Rows that contain multi-line values have sufficient height to display all lines of any cell contained within

Versions

Python 3.7.5 StyleFrame 3.0.5 pandas 1.0.4 openpyxl 2.6.3

Described behaviour also present with pandas 1.1.3 and openpyxl 3.0.5

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
DeepSpace2commented, Sep 21, 2021

@coolbeam Indeed, sf.data_df[column].astype(str).str.count('\n') should be sf.data_df[column].astype(str).str.count('\n').max(). I will edit the original comment.

As for your second point, I think that should be a separate issue as I’m not sure this is related to this. Anyway, please provide a reproducible example as I’m not sure I follow.

0reactions
buhtzcommented, Jul 14, 2022

Maybe related #131

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change and AutoFit row height in Excel
Method 2. On the Home tab, in the Cells group, click Format > AutoFit Row Height: Tip. To auto fit all rows on...
Read more >
Change the column width and row height
Under Cell Size, click AutoFit Column Width. Note: To quickly autofit all columns on the worksheet, click the Select All button, and then...
Read more >
Excel AUTOFIT: Make Rows/Columns Fit the Text ...
Autofit Row Height : This feature automatically adjusts the row height to fit the text in the cell. You can autofit multiple rows...
Read more >
How to shrink column width with multiple-line custom ...
Select the cell with the value. · Hit Ctrl + 1 to open the Number Formats dialog. · Choose the Custom category and...
Read more >
How to make Excel's "Auto Fit Row Height" feature actually ...
Resizing the column width as other people have suggested will resolve that cell's issue, but if your spreadsheet has lots of varying size...
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