openpyxl-style-writer


Nameopenpyxl-style-writer JSON
Version 1.1.2 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-02-29 08:37:39
maintainer
docs_urlNone
authorZncl2222
requires_python>=3.6
licenseMIT
keywords openpyxl excel style
VCS
bugtrack_url
requirements openpyxl pytest pytest-cov
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![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)


# openpyxl_style_writer
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
)
```

## 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)


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.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": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "openpyxl,excel,style",
    "author": "Zncl2222",
    "author_email": "zwebapplication@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/5a/30/022ab97f4932f367f95331a5bd93132cba7206c2cd827e279a9c4a0e9ac1/openpyxl_style_writer-1.1.2.tar.gz",
    "platform": null,
    "description": "![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\n\n# openpyxl_style_writer\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```$ pip install openpyxl_style_writer ```\n\n# Usage\n### Example\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\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\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.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\n## List of Key words in openpyxl_style_writer\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.2",
    "project_urls": {
        "Homepage": "https://github.com/Zncl2222/openpyxl_style_writer"
    },
    "split_keywords": [
        "openpyxl",
        "excel",
        "style"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1ee04fd4068a817bf66419374a80da72f7481649260fff40aff51922dc71885b",
                "md5": "090b3036bd11687e62afc08d08716f5d",
                "sha256": "48134849f18d5c714e2570d3da49f1dcb592472a1025eb3ddee5483f2251e882"
            },
            "downloads": -1,
            "filename": "openpyxl_style_writer-1.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "090b3036bd11687e62afc08d08716f5d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7398,
            "upload_time": "2024-02-29T08:37:38",
            "upload_time_iso_8601": "2024-02-29T08:37:38.912775Z",
            "url": "https://files.pythonhosted.org/packages/1e/e0/4fd4068a817bf66419374a80da72f7481649260fff40aff51922dc71885b/openpyxl_style_writer-1.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5a30022ab97f4932f367f95331a5bd93132cba7206c2cd827e279a9c4a0e9ac1",
                "md5": "1e41d8a1c6c77cc9530ef07df9ac062f",
                "sha256": "02b82dd098ac0a0466a2947334b9bdb33a8d906ee509c9b8b74937257b0dfc6e"
            },
            "downloads": -1,
            "filename": "openpyxl_style_writer-1.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "1e41d8a1c6c77cc9530ef07df9ac062f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 7063,
            "upload_time": "2024-02-29T08:37:39",
            "upload_time_iso_8601": "2024-02-29T08:37:39.925943Z",
            "url": "https://files.pythonhosted.org/packages/5a/30/022ab97f4932f367f95331a5bd93132cba7206c2cd827e279a9c4a0e9ac1/openpyxl_style_writer-1.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-29 08:37:39",
    "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": [
        {
            "name": "openpyxl",
            "specs": []
        },
        {
            "name": "pytest",
            "specs": []
        },
        {
            "name": "pytest-cov",
            "specs": []
        }
    ],
    "lcname": "openpyxl-style-writer"
}
        
Elapsed time: 0.20851s