openpyxl-style-writer


Nameopenpyxl-style-writer JSON
Version 1.1.4 PyPI version JSON
download
home_pagehttps://github.com/Zncl2222/openpyxl_style_writer
SummaryA wrapper for openpyxl to create and use resualbe style in write only mode
upload_time2024-07-01 16:10:45
maintainerNone
docs_urlNone
authorZncl2222
requires_python>=3.6
licenseMIT
keywords openpyxl excel style
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # openpyxl_style_writer

![licence](https://img.shields.io/github/license/Zncl2222/openpyxl_style_writer)
[![ci](https://img.shields.io/github/actions/workflow/status/Zncl2222/openpyxl_style_writer/github-pre-commit.yml?logo=pre-commit&label=pre-commit)](https://github.com/Zncl2222/openpyxl_style_writer/actions/workflows/github-pre-commit.yml)
[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Zncl2222/openpyxl_style_writer/pytest.yml?logo=pytest&label=pytest)](https://github.com/Zncl2222/openpyxl_style_writer/actions/workflows/pytest.yml)
[![codecov](https://codecov.io/gh/Zncl2222/openpyxl_style_writer/graph/badge.svg?token=UP6M8SCC14)](https://codecov.io/gh/Zncl2222/openpyxl_style_writer)
![language](https://img.shields.io/badge/Solutions-black.svg?style=flat&logo=python)

This is a wrapper base on [openpyxl](https://pypi.org/project/openpyxl/) package. The original feature to create resuable style ([NameStyled](https://openpyxl.readthedocs.io/en/stable/styles.html#creating-a-named-style)) is not avaliable for [write only mode](https://openpyxl.readthedocs.io/en/stable/optimized.html#write-only-mode). Thus this package aimed to provide a easy way for user to create resuable styles and use it on [write only mode](https://openpyxl.readthedocs.io/en/stable/optimized.html#write-only-mode) easily

# Installation

```$ pip install openpyxl_style_writer ```

# Usage

### Example

```python
from openpyxl_style_writer import CustomStyle, DefaultStyle, RowWriter


if __name__ == '__main__':
    workbook = RowWriter()
    # enable protection by protection=True
    workbook.create_sheet('ExampleSheet', protection=True)

    title = 'This is an example'
    row_title_1 = ['fruits', 'fruits', 'animals', 'animals']
    row_title_2 = ['apple', 'banana', 'cat', 'dog']
    percent_data = [0.1, 0.6, 0.225, 0.4755, 0.9, 1]
    data = [10, 20, 30, 40]

    # append single cell with Default Style
    workbook.row_append(title)
    workbook.set_cell_width(1, 30)
    workbook.create_row()
    for item in row_title_1:
        workbook.row_append(item)
    workbook.create_row()

    # set custom Default Style and append list in a row
    blue_font_style = {'color': '0000ff', 'bold': True, 'size': 8}
    DefaultStyle.set_default(font_params=blue_font_style)
    workbook.row_append_list(row_title_2)
    workbook.create_row()

    # create new Custom Style and give row_append_list a style
    pink_fill_style = {'patternType': 'solid', 'fgColor': 'd25096'}
    # add protect to pink_style
    pink_style = CustomStyle(fill_params=pink_fill_style, protect=True)
    workbook.row_append_list(data, pink_style)
    workbook.create_row()

    # create number_format style
    percent_style = CustomStyle(font_size=8, number_format='0.0%')
    workbook.row_append_list(percent_data, percent_style)
    workbook.create_row()

    workbook.save('example.xlsx')
```

### CustomStyle & DefaultStyle
You can either set the `CustomStyle` and `DefaultStyle` by the key words from openpyxl or from this package. Here is an example to create a `CustomStyle` by the openpyxl key words.

```python
from openpyxl_style_writer import CustomStyle

# Create the style by the original key words from openpyxl

blue_title_font = {
    'color': '0000ff',
    'bold': True,
    'size': 15,
}
cyan_title_pattern = {
    'patternType': 'solid',
    'fgColor': '00ffff	'
}

# Use font_params and fill_params to create a reusable style with the blue font and cyan fill.
# The style you do not set will use the style of DefaultStyle.

custom_title_style = CustomStyle(
    font_params=blue_title_font,
    fill_params=cyan_titl_patter,
)
```
You could also set your `DefaultStyle` first, and the rest of the `CustomStyle` will follow the settings of `DefaultStyle` if the style do not set in `CustomStyle`.

```python
from openpyxl_style_writer import CustomStyle, DefaultStyle

cyan_title_pattern = {
    'patternType': 'solid',
    'fgColor': '00ffff	'
}

blue_title_font = {
    'color': '0000ff',
    'bold': True,
    'size': 15,
}

# set default style with cyan fill
DefaultStyle.set_default(fill_params=cyan_title_pattern)

# This custom style will show blue font and cyan fill, although it only set the font_params
custom_title_style = CustomStyle(
    font_params=blue_title_font,
)
```

If you want to do it in simple way, openpyxl_style_writer offer a map for some [common key words](#list-of-key-words-in-openpyxl_style_writer). You can use the key words of openpyxl_style_writer.
```python
from openpyxl_style_writer import CustomStyle

custom_title_style = CustomStyle(
    font_size=15,
    font_name='Calibri',
)
```

Or you can use both methods
```python
from openpyxl_style_writer import CustomStyle

blue_title_font = {
    'color': '0000ff',
    'bold': True,
    'size': 15,
}

custom_title_style = CustomStyle(
    font_size=15,
    font_name='Calibri',
    fill_params=cyan_title_pattern
)
```

If you need to declare multiple styles, and many of them are very similar, you
can use the `clone_and_modify` function to streamline the process. For example,
if you need to create blue_title_style, red_title_style, and green_title_style,
the straightforward method involves creating each one individually:

```python
blue_title_style = CustomStyle(
    font_bold=True,
    font_color='0000ff',
    font_size=15
)
red_title_style = CustomStyle(
    font_bold=True,
    font_color='ff0000',
    font_size=15
)
green_title_style = CustomStyle(
    font_bold=True,
    font_color='00ff00',
    font_size=15
)
```

However, with the clone_and_modify method, you can create them more efficiently:

```python
blue_title_style = CustomStyle(
    font_bold=True,
    font_color='0000ff',
    font_size=15
)
red_title_style = blue_title_style.clone_and_modify(font_color='ff0000')
green_title_style = blue_title_style.clone_and_modify(font_color='00ff00')
```

This approach is especially helpful when there are many parameters to input in
a CustomStyle instance.

## Advanced Usage

In scenarios where you want to establish a collection of reusable styles for your Excel documents or if you have a variety of different Excel outputs, you can define a base class containing multiple custom styles and then inherit from this base class in your Excel class.

The following example demonstrates how to achieve this using the openpyxl_style_writer library:


```python
from openpyxl_style_writer import CustomStyle, RowWriter


class BaseExcelWriter(RowWriter):
    blue_font = {
        'color': '0000ff',
        'bold': True,
        'size': 15,
    }
    cyan_title_pattern = {
        'patternType': 'solid',
        'fgColor': '00ffff	'
    }
    blue_font_style = CustomStyle(font_params=blue_font)
    cyan_fill_style = CustomStyle(fill_params=cyan_title_pattern)
    # Clone a existed style and modify font params
    cyan_fill_with_blue_font = cyan_fill_style.cloned_and_modify(
        font_params=blue_font
    )


class ExampleExcel(BaseExcelWriter):
    first_row = ['Apple', 'Banana', 'Cat']

    def create(self, file_name='output.xlsx'):
        self.create_sheet('ExampleSheet', protection=True)
        self.row_append_list(self.first_row, style=self.cyan_fill_style)
        self.create_row()
        for idx, _ in enumerate(self.first_row):
            self.row_append(idx, style=self.blue_font_style)
        self.row_append(
            'Clone_and_modify_cell',
            style=self.cyan_fill_with_blue_font
        )
        self.create_row()
        self.save(file_name)


class ExampleExcel2(BaseExcelWriter):

    def create(self, file_name='output.xlsx'):
        # create a content of excel2...

if __name__ == '__main__':
    example = ExampleExcel()
    example.create('example.xlsx')
```

## List of Key words in openpyxl_style_writer

This is a list of the key words in openpyxl_style_writer and how it map to the attributes of openpyxl

<div align='center'>

| **class**     |       Key           |  datatype   |          **map to**                                                   |
| :------------ | ------------------- | ----------- | :-------------------------------------------------------------------  |
| **font**      | font_size           | int         | openpyxl.styles.Font.size                                             |
|               | font_name           | str         | openpyxl.styles.Font.name                                             |
|               | font_bold           | bool        | openpyxl.styles.Font.bold                                             |
|               | font_italic         | bool        | openpyxl.styles.Font.italic                                           |
|               | font_underline      | str         | openpyxl.styles.Font.underline                                        |
|               | font_strike         | bool        | openpyxl.styles.Font.strike                                           |
|               | font_vertAlign      | str         | openpyxl.styles.Font.vertAlign                                        |
|               | font_color          | str         | openpyxl.styles.Font.color                                            |
| **fill**      | fill_color          | str         | openpyxl.styles.PatternFill.color                                     |
| **alignment** | ali_horizontal      | str         | openpyxl.styles.Alignment.color                                       |
|               | ali_vertical        | str         | openpyxl.styles.Alignment.color                                       |
|               | ali_wrap_text       | str         | openpyxl.styles.Alignment.color                                       |
| **border**    | border_style_top    | str         | openpyxl.styles.Border.top with openpyxl.styles.Side.border_style     |
|               | border_style_right  | str         | openpyxl.styles.Border.right with openpyxl.styles.Side.border_style   |
|               | border_style_left   | str         | openpyxl.styles.Border.left with openpyxl.styles.Side.border_style    |
|               | border_style_bottom | str         | openpyxl.styles.Border.bottom with openpyxl.styles.Side.border_style  |
|               | border_color_top    | str         | openpyxl.styles.Border.top with openpyxl.styles.Side.color            |
|               | border_color_right  | str         | openpyxl.styles.Border.right with openpyxl.styles.Side.color          |
|               | border_color_left   | str         | openpyxl.styles.Border.left with openpyxl.styles.Side.color           |
|               | border_color_bottom | str         | openpyxl.styles.Border.bottom with openpyxl.styles.Side.color         |

</div>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Zncl2222/openpyxl_style_writer",
    "name": "openpyxl-style-writer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "openpyxl, excel, style",
    "author": "Zncl2222",
    "author_email": "zwebapplication@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6c/6c/26457d7080caf3027437c5fb83e28d89dd63c1f73db6a5e1af26170ad784/openpyxl_style_writer-1.1.4.tar.gz",
    "platform": null,
    "description": "# openpyxl_style_writer\n\n![licence](https://img.shields.io/github/license/Zncl2222/openpyxl_style_writer)\n[![ci](https://img.shields.io/github/actions/workflow/status/Zncl2222/openpyxl_style_writer/github-pre-commit.yml?logo=pre-commit&label=pre-commit)](https://github.com/Zncl2222/openpyxl_style_writer/actions/workflows/github-pre-commit.yml)\n[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Zncl2222/openpyxl_style_writer/pytest.yml?logo=pytest&label=pytest)](https://github.com/Zncl2222/openpyxl_style_writer/actions/workflows/pytest.yml)\n[![codecov](https://codecov.io/gh/Zncl2222/openpyxl_style_writer/graph/badge.svg?token=UP6M8SCC14)](https://codecov.io/gh/Zncl2222/openpyxl_style_writer)\n![language](https://img.shields.io/badge/Solutions-black.svg?style=flat&logo=python)\n\nThis is a wrapper base on [openpyxl](https://pypi.org/project/openpyxl/) package. The original feature to create resuable style ([NameStyled](https://openpyxl.readthedocs.io/en/stable/styles.html#creating-a-named-style)) is not avaliable for [write only mode](https://openpyxl.readthedocs.io/en/stable/optimized.html#write-only-mode). Thus this package aimed to provide a easy way for user to create resuable styles and use it on [write only mode](https://openpyxl.readthedocs.io/en/stable/optimized.html#write-only-mode) easily\n\n# Installation\n\n```$ pip install openpyxl_style_writer ```\n\n# Usage\n\n### Example\n\n```python\nfrom openpyxl_style_writer import CustomStyle, DefaultStyle, RowWriter\n\n\nif __name__ == '__main__':\n    workbook = RowWriter()\n    # enable protection by protection=True\n    workbook.create_sheet('ExampleSheet', protection=True)\n\n    title = 'This is an example'\n    row_title_1 = ['fruits', 'fruits', 'animals', 'animals']\n    row_title_2 = ['apple', 'banana', 'cat', 'dog']\n    percent_data = [0.1, 0.6, 0.225, 0.4755, 0.9, 1]\n    data = [10, 20, 30, 40]\n\n    # append single cell with Default Style\n    workbook.row_append(title)\n    workbook.set_cell_width(1, 30)\n    workbook.create_row()\n    for item in row_title_1:\n        workbook.row_append(item)\n    workbook.create_row()\n\n    # set custom Default Style and append list in a row\n    blue_font_style = {'color': '0000ff', 'bold': True, 'size': 8}\n    DefaultStyle.set_default(font_params=blue_font_style)\n    workbook.row_append_list(row_title_2)\n    workbook.create_row()\n\n    # create new Custom Style and give row_append_list a style\n    pink_fill_style = {'patternType': 'solid', 'fgColor': 'd25096'}\n    # add protect to pink_style\n    pink_style = CustomStyle(fill_params=pink_fill_style, protect=True)\n    workbook.row_append_list(data, pink_style)\n    workbook.create_row()\n\n    # create number_format style\n    percent_style = CustomStyle(font_size=8, number_format='0.0%')\n    workbook.row_append_list(percent_data, percent_style)\n    workbook.create_row()\n\n    workbook.save('example.xlsx')\n```\n\n### CustomStyle & DefaultStyle\nYou can either set the `CustomStyle` and `DefaultStyle` by the key words from openpyxl or from this package. Here is an example to create a `CustomStyle` by the openpyxl key words.\n\n```python\nfrom openpyxl_style_writer import CustomStyle\n\n# Create the style by the original key words from openpyxl\n\nblue_title_font = {\n    'color': '0000ff',\n    'bold': True,\n    'size': 15,\n}\ncyan_title_pattern = {\n    'patternType': 'solid',\n    'fgColor': '00ffff\t'\n}\n\n# Use font_params and fill_params to create a reusable style with the blue font and cyan fill.\n# The style you do not set will use the style of DefaultStyle.\n\ncustom_title_style = CustomStyle(\n    font_params=blue_title_font,\n    fill_params=cyan_titl_patter,\n)\n```\nYou could also set your `DefaultStyle` first, and the rest of the `CustomStyle` will follow the settings of `DefaultStyle` if the style do not set in `CustomStyle`.\n\n```python\nfrom openpyxl_style_writer import CustomStyle, DefaultStyle\n\ncyan_title_pattern = {\n    'patternType': 'solid',\n    'fgColor': '00ffff\t'\n}\n\nblue_title_font = {\n    'color': '0000ff',\n    'bold': True,\n    'size': 15,\n}\n\n# set default style with cyan fill\nDefaultStyle.set_default(fill_params=cyan_title_pattern)\n\n# This custom style will show blue font and cyan fill, although it only set the font_params\ncustom_title_style = CustomStyle(\n    font_params=blue_title_font,\n)\n```\n\nIf you want to do it in simple way, openpyxl_style_writer offer a map for some [common key words](#list-of-key-words-in-openpyxl_style_writer). You can use the key words of openpyxl_style_writer.\n```python\nfrom openpyxl_style_writer import CustomStyle\n\ncustom_title_style = CustomStyle(\n    font_size=15,\n    font_name='Calibri',\n)\n```\n\nOr you can use both methods\n```python\nfrom openpyxl_style_writer import CustomStyle\n\nblue_title_font = {\n    'color': '0000ff',\n    'bold': True,\n    'size': 15,\n}\n\ncustom_title_style = CustomStyle(\n    font_size=15,\n    font_name='Calibri',\n    fill_params=cyan_title_pattern\n)\n```\n\nIf you need to declare multiple styles, and many of them are very similar, you\ncan use the `clone_and_modify` function to streamline the process. For example,\nif you need to create blue_title_style, red_title_style, and green_title_style,\nthe straightforward method involves creating each one individually:\n\n```python\nblue_title_style = CustomStyle(\n    font_bold=True,\n    font_color='0000ff',\n    font_size=15\n)\nred_title_style = CustomStyle(\n    font_bold=True,\n    font_color='ff0000',\n    font_size=15\n)\ngreen_title_style = CustomStyle(\n    font_bold=True,\n    font_color='00ff00',\n    font_size=15\n)\n```\n\nHowever, with the clone_and_modify method, you can create them more efficiently:\n\n```python\nblue_title_style = CustomStyle(\n    font_bold=True,\n    font_color='0000ff',\n    font_size=15\n)\nred_title_style = blue_title_style.clone_and_modify(font_color='ff0000')\ngreen_title_style = blue_title_style.clone_and_modify(font_color='00ff00')\n```\n\nThis approach is especially helpful when there are many parameters to input in\na CustomStyle instance.\n\n## Advanced Usage\n\nIn scenarios where you want to establish a collection of reusable styles for your Excel documents or if you have a variety of different Excel outputs, you can define a base class containing multiple custom styles and then inherit from this base class in your Excel class.\n\nThe following example demonstrates how to achieve this using the openpyxl_style_writer library:\n\n\n```python\nfrom openpyxl_style_writer import CustomStyle, RowWriter\n\n\nclass BaseExcelWriter(RowWriter):\n    blue_font = {\n        'color': '0000ff',\n        'bold': True,\n        'size': 15,\n    }\n    cyan_title_pattern = {\n        'patternType': 'solid',\n        'fgColor': '00ffff\t'\n    }\n    blue_font_style = CustomStyle(font_params=blue_font)\n    cyan_fill_style = CustomStyle(fill_params=cyan_title_pattern)\n    # Clone a existed style and modify font params\n    cyan_fill_with_blue_font = cyan_fill_style.cloned_and_modify(\n        font_params=blue_font\n    )\n\n\nclass ExampleExcel(BaseExcelWriter):\n    first_row = ['Apple', 'Banana', 'Cat']\n\n    def create(self, file_name='output.xlsx'):\n        self.create_sheet('ExampleSheet', protection=True)\n        self.row_append_list(self.first_row, style=self.cyan_fill_style)\n        self.create_row()\n        for idx, _ in enumerate(self.first_row):\n            self.row_append(idx, style=self.blue_font_style)\n        self.row_append(\n            'Clone_and_modify_cell',\n            style=self.cyan_fill_with_blue_font\n        )\n        self.create_row()\n        self.save(file_name)\n\n\nclass ExampleExcel2(BaseExcelWriter):\n\n    def create(self, file_name='output.xlsx'):\n        # create a content of excel2...\n\nif __name__ == '__main__':\n    example = ExampleExcel()\n    example.create('example.xlsx')\n```\n\n## List of Key words in openpyxl_style_writer\n\nThis is a list of the key words in openpyxl_style_writer and how it map to the attributes of openpyxl\n\n<div align='center'>\n\n| **class**     |       Key           |  datatype   |          **map to**                                                   |\n| :------------ | ------------------- | ----------- | :-------------------------------------------------------------------  |\n| **font**      | font_size           | int         | openpyxl.styles.Font.size                                             |\n|               | font_name           | str         | openpyxl.styles.Font.name                                             |\n|               | font_bold           | bool        | openpyxl.styles.Font.bold                                             |\n|               | font_italic         | bool        | openpyxl.styles.Font.italic                                           |\n|               | font_underline      | str         | openpyxl.styles.Font.underline                                        |\n|               | font_strike         | bool        | openpyxl.styles.Font.strike                                           |\n|               | font_vertAlign      | str         | openpyxl.styles.Font.vertAlign                                        |\n|               | font_color          | str         | openpyxl.styles.Font.color                                            |\n| **fill**      | fill_color          | str         | openpyxl.styles.PatternFill.color                                     |\n| **alignment** | ali_horizontal      | str         | openpyxl.styles.Alignment.color                                       |\n|               | ali_vertical        | str         | openpyxl.styles.Alignment.color                                       |\n|               | ali_wrap_text       | str         | openpyxl.styles.Alignment.color                                       |\n| **border**    | border_style_top    | str         | openpyxl.styles.Border.top with openpyxl.styles.Side.border_style     |\n|               | border_style_right  | str         | openpyxl.styles.Border.right with openpyxl.styles.Side.border_style   |\n|               | border_style_left   | str         | openpyxl.styles.Border.left with openpyxl.styles.Side.border_style    |\n|               | border_style_bottom | str         | openpyxl.styles.Border.bottom with openpyxl.styles.Side.border_style  |\n|               | border_color_top    | str         | openpyxl.styles.Border.top with openpyxl.styles.Side.color            |\n|               | border_color_right  | str         | openpyxl.styles.Border.right with openpyxl.styles.Side.color          |\n|               | border_color_left   | str         | openpyxl.styles.Border.left with openpyxl.styles.Side.color           |\n|               | border_color_bottom | str         | openpyxl.styles.Border.bottom with openpyxl.styles.Side.color         |\n\n</div>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A wrapper for openpyxl to create and use resualbe style in write only mode",
    "version": "1.1.4",
    "project_urls": {
        "Homepage": "https://github.com/Zncl2222/openpyxl_style_writer"
    },
    "split_keywords": [
        "openpyxl",
        " excel",
        " style"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8396b261a865b65d0a34f2e5cf7c5deff40fb1332f8a55f6cb87a9a353ed2694",
                "md5": "59cc80765a3eb3e50ac8de36049ee7a1",
                "sha256": "0a7fde57a14c345160a79038f30b2bb624d08dd5e6022a61ec976d932fd51088"
            },
            "downloads": -1,
            "filename": "openpyxl_style_writer-1.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "59cc80765a3eb3e50ac8de36049ee7a1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7850,
            "upload_time": "2024-07-01T16:10:44",
            "upload_time_iso_8601": "2024-07-01T16:10:44.501094Z",
            "url": "https://files.pythonhosted.org/packages/83/96/b261a865b65d0a34f2e5cf7c5deff40fb1332f8a55f6cb87a9a353ed2694/openpyxl_style_writer-1.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c6c26457d7080caf3027437c5fb83e28d89dd63c1f73db6a5e1af26170ad784",
                "md5": "3724d2ecfc5afaedc9cd5c5d3cc7e5fe",
                "sha256": "687e7ee61cb034f4063c832f94b9ef812a2d6bbe34d02150cc26e78f9e6502a6"
            },
            "downloads": -1,
            "filename": "openpyxl_style_writer-1.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3724d2ecfc5afaedc9cd5c5d3cc7e5fe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 9757,
            "upload_time": "2024-07-01T16:10:45",
            "upload_time_iso_8601": "2024-07-01T16:10:45.812584Z",
            "url": "https://files.pythonhosted.org/packages/6c/6c/26457d7080caf3027437c5fb83e28d89dd63c1f73db6a5e1af26170ad784/openpyxl_style_writer-1.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-01 16:10:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Zncl2222",
    "github_project": "openpyxl_style_writer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "openpyxl-style-writer"
}
        
Elapsed time: 4.29114s