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

Feature request: Allow injection of formatting parameters, e.g. wordWrap=True, minColWidth='200px' #5

Open
patarapolw opened this issue Aug 5, 2018 · 3 comments

Comments

@patarapolw
Copy link

patarapolw commented Aug 5, 2018

Or wordWrap=False, of course.

Or, for example, putting *.config.json as environmental variable, or in the same path as file.xlsx.

As a matter of fact, I am trying to do this:-

@staticmethod
def to_styles(wb):
    """

    :param openpyxl.workbook.workbook.Workbook wb:
    :return:
    """
    styles = dict()

    for sheet_name in wb.sheetnames:
        ws = wb[sheet_name]

        styles[sheet_name] = {
            'worksheet': {
                'freeze_panes': ws.freeze_panes,
                'column_widths': [],
                'row_heights': []
            },
            'format': []
        }

        for i, col in enumerate(ws.iter_cols()):
            styles[sheet_name]['worksheet']['column_widths']\
                .append(ws.column_dimensions[get_column_letter(i + 1)].width)

        for i, row in enumerate(ws):
            styles[sheet_name]['worksheet']['row_heights'] \
                .append(ws.row_dimensions[i + 1].height)

            row_format = []
            for cell in row:
                font = cell.font
                font_color = font.color

                alignment = cell.alignment
                fill = cell.fill
                border = cell.border

                cell_format = {
                    'font': {
                        'font_name': font.name,
                        'font_size': font.sz,
                        'font_color': font_color.rgb,
                        'bold': font.b,
                        'italic': font.i,
                        'underline': font.u,
                        'font_strikeout': font.strike,
                        # 'font_script':
                    },
                    'number': {
                        'num_format': cell.number_format
                    },
                    # 'Protection':
                    'alignment': {
                        'align': alignment.horizontal,
                        'valign': alignment.vertical,
                        'rotation': alignment.textRotation,
                        'text_wrap': alignment.wrapText,
                        'reading_order': alignment.readingOrder,
                        'text_justlast': alignment.justifyLastLine,
                        # 'center_across':,
                        'indent': alignment.relativeIndent,
                        'shrink': alignment.shrinkToFit
                    },
                    'pattern': {
                        'pattern': fill.patternType,
                        'bg_color': fill.bgColor.rgb,
                        'fg_color': fill.fgColor.rgb
                    },
                    'border': {
                        # 'border':,
                        # 'border_color':
                        'bottom': border.bottom.style,
                        'bottom_color': border.bottom.color.rgb,
                        'top': border.top.style,
                        'top_color': border.top.color.rgb,
                        'left': border.left.style,
                        'left_color': border.left.color.rgb,
                        'right': border.right.style,
                        'right_color': border.right.color.rgb
                    }
                }

                row_format.append(cell_format)

            styles[sheet_name]['format'].append(row_format)

    return styles

But syncing with openpyxl can be fragile, and is a little overkill. Also, make reading files slow.

This should easily contain (almost) all possible formatting, and make Excel output beautiful by default.

@patarapolw patarapolw changed the title Feature request: Allow injection of formatting parameters, i.e. wordWrap=True for all cells. Feature request: Allow injection of formatting parameters, i.e. wordWrap=True, minColWidth='200px' for all cells. Aug 5, 2018
@patarapolw patarapolw changed the title Feature request: Allow injection of formatting parameters, i.e. wordWrap=True, minColWidth='200px' for all cells. Feature request: Allow injection of formatting parameters, e.g. wordWrap=True, minColWidth='200px' for all cells. Aug 5, 2018
@patarapolw patarapolw changed the title Feature request: Allow injection of formatting parameters, e.g. wordWrap=True, minColWidth='200px' for all cells. Feature request: Allow injection of formatting parameters, e.g. wordWrap=True, minColWidth='200px' Aug 5, 2018
@patarapolw
Copy link
Author

patarapolw commented Aug 6, 2018

As a matter of fact, I have already created one... https://github.com/patarapolw/pyexcel-xlsxwx, but I feel it is not as memory efficient as yours...

@chfw
Copy link
Member

chfw commented Aug 6, 2018

cool, that means I have done something right! Here is the magic: https://github.com/pyexcel/pyexcel-xlsxw/blob/master/pyexcel_xlsxw/xlsxw.py#L54

@chfw
Copy link
Member

chfw commented Aug 6, 2018

And if you write a bit more, your pyexcel-xlsxwx can be loaded by pyexcel as external plugin.

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

No branches or pull requests

2 participants