Skip to content

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

@nachtkinder

Description

@nachtkinder

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions