Skip to main content

A wrapper for openpyxl to create and use resualbe style in write only mode

Project description

licence ci GitHub Workflow Status (with event) codecov language

openpyxl_style_writer

This is a wrapper base on openpyxl package. The original feature to create resuable style (NameStyled) is not avaliable for 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 easily

Installation

$ pip install openpyxl_style_writer

Usuage

Example

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.

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.

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. You can use the key words of openpyxl_style_writer.

from openpyxl_style_writer import CustomStyle

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

Or you can use both methods

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 Usuage

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:

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openpyxl_style_writer-1.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

openpyxl_style_writer-1.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file openpyxl_style_writer-1.1.1.tar.gz.

File metadata

  • Download URL: openpyxl_style_writer-1.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for openpyxl_style_writer-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8b11ba34c3894f42701d904c6f6b57a980ecee3869d50b06077f1d673fd41a59
MD5 5637bb29571bd722b8852045c8cbe0bf
BLAKE2b-256 76e04eaac25fcbcd2bb9791d92cd9072a36fd9cc8fca0c14400854a85a4fd3d3

See more details on using hashes here.

File details

Details for the file openpyxl_style_writer-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openpyxl_style_writer-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b01cc96797d022044ce6c23773b40af48f6342ef9c9d465db69535746f0ef7b
MD5 44fbaff902358279ba5859042d1746d5
BLAKE2b-256 3f241f8ffede2843a053127ca32502ed493ddeeccfd20ea22c394f3f13f04b08

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page