libxlsxwriter
|
This section describes the functions and properties that are available for formatting cells in Excel.
The properties of a cell that can be formatted include: fonts, colors, patterns, borders, alignment and number formatting.
Formats in libxlsxwriter
are accessed via the lxw_format struct. Throughout this document these will be referred to simply as Formats.
Formats are created by calling the workbook_add_format() method as follows:
The members of the lxw_format struct aren't modified directly. Instead the format properties are set by calling the functions shown in this section. For example:
The full range of formatting options that can be applied using libxlsxwriter
are shown below.
Functions | |
void | format_set_font_name (lxw_format *format, const char *font_name) |
Set the font used in the cell. | |
void | format_set_font_size (lxw_format *format, double size) |
Set the size of the font used in the cell. | |
void | format_set_font_color (lxw_format *format, lxw_color_t color) |
Set the color of the font used in the cell. | |
void | format_set_bold (lxw_format *format) |
Turn on bold for the format font. | |
void | format_set_italic (lxw_format *format) |
Turn on italic for the format font. | |
void | format_set_underline (lxw_format *format, uint8_t style) |
Turn on underline for the format: | |
void | format_set_font_strikeout (lxw_format *format) |
Set the strikeout property of the font. | |
void | format_set_font_script (lxw_format *format, uint8_t style) |
Set the superscript/subscript property of the font. | |
void | format_set_num_format (lxw_format *format, const char *num_format) |
Set the number format for a cell. | |
void | format_set_num_format_index (lxw_format *format, uint8_t index) |
Set the Excel built-in number format for a cell. | |
void | format_set_unlocked (lxw_format *format) |
Set the cell unlocked state. | |
void | format_set_hidden (lxw_format *format) |
Hide formulas in a cell. | |
void | format_set_align (lxw_format *format, uint8_t alignment) |
Set the alignment for data in the cell. | |
void | format_set_text_wrap (lxw_format *format) |
Wrap text in a cell. | |
void | format_set_rotation (lxw_format *format, int16_t angle) |
Set the rotation of the text in a cell. | |
void | format_set_indent (lxw_format *format, uint8_t level) |
Set the cell text indentation level. | |
void | format_set_shrink (lxw_format *format) |
Turn on the text "shrink to fit" for a cell. | |
void | format_set_pattern (lxw_format *format, uint8_t index) |
Set the background fill pattern for a cell. | |
void | format_set_bg_color (lxw_format *format, lxw_color_t color) |
Set the pattern background color for a cell. | |
void | format_set_fg_color (lxw_format *format, lxw_color_t color) |
Set the pattern foreground color for a cell. | |
void | format_set_border (lxw_format *format, uint8_t style) |
Set the cell border style. | |
void | format_set_bottom (lxw_format *format, uint8_t style) |
Set the cell bottom border style. | |
void | format_set_top (lxw_format *format, uint8_t style) |
Set the cell top border style. | |
void | format_set_left (lxw_format *format, uint8_t style) |
Set the cell left border style. | |
void | format_set_right (lxw_format *format, uint8_t style) |
Set the cell right border style. | |
void | format_set_border_color (lxw_format *format, lxw_color_t color) |
Set the color of the cell border. | |
void | format_set_bottom_color (lxw_format *format, lxw_color_t color) |
Set the color of the bottom cell border. | |
void | format_set_top_color (lxw_format *format, lxw_color_t color) |
Set the color of the top cell border. | |
void | format_set_left_color (lxw_format *format, lxw_color_t color) |
Set the color of the left cell border. | |
void | format_set_right_color (lxw_format *format, lxw_color_t color) |
Set the color of the right cell border. | |
void | format_set_diag_type (lxw_format *format, uint8_t type) |
Set the diagonal cell border type. | |
void | format_set_diag_border (lxw_format *format, uint8_t style) |
Set the diagonal cell border style. | |
void | format_set_diag_color (lxw_format *format, lxw_color_t color) |
Set the diagonal cell border color. | |
void | format_set_quote_prefix (lxw_format *format) |
Turn on quote prefix for the format. | |
void format_set_font_name | ( | lxw_format * | format, |
const char * | font_name | ||
) |
format | Pointer to a Format instance. |
font_name | Cell font name. |
Specify the font used used in the cell format:
Excel can only display fonts that are installed on the system that it is running on. Therefore it is generally best to use the fonts that come as standard with Excel such as Calibri, Times New Roman and Courier New.
The default font in Excel 2007, and later, is Calibri.
void format_set_font_size | ( | lxw_format * | format, |
double | size | ||
) |
format | Pointer to a Format instance. |
size | The cell font size. |
Set the font size of the cell format:
Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the worksheet_set_row() function.
void format_set_font_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell font color. |
Set the font color:
The color should be an RGB integer value, see Working with Colors.
void format_set_bold | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
Set the bold property of the font:
void format_set_italic | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
Set the italic property of the font:
void format_set_underline | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Underline style. |
Set the underline property of the format:
The available underline styles are:
void format_set_font_strikeout | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
void format_set_font_script | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Superscript or subscript style. |
Set the superscript o subscript property of the font.
The available script styles are:
void format_set_num_format | ( | lxw_format * | format, |
const char * | num_format | ||
) |
format | Pointer to a Format instance. |
num_format | The cell number format string. |
This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format.
The numerical format of a cell can be specified by using a format string:
Format strings can control any aspect of number formatting allowed by Excel:
To set a number format that matches an Excel format category such as "Date" or "Currency" see Number Format Categories.
The number system used for dates is described in Working with Dates and Times.
For more information on number formats in Excel refer to the Microsoft documentation on cell formats.
void format_set_num_format_index | ( | lxw_format * | format, |
uint8_t | index | ||
) |
format | Pointer to a Format instance. |
index | The built-in number format index for the cell. |
This function is similar to format_set_num_format() except that it takes an index to a limited number of Excel's built-in number formats instead of a user defined format string:
The Excel built-in number formats as shown in the table below:
Index | Index | Format String |
---|---|---|
0 | 0x00 | General |
1 | 0x01 | 0 |
2 | 0x02 | 0.00 |
3 | 0x03 | #,##0 |
4 | 0x04 | #,##0.00 |
5 | 0x05 | ($#,##0_);($#,##0) |
6 | 0x06 | ($#,##0_);[Red]($#,##0) |
7 | 0x07 | ($#,##0.00_);($#,##0.00) |
8 | 0x08 | ($#,##0.00_);[Red]($#,##0.00) |
9 | 0x09 | 0% |
10 | 0x0a | 0.00% |
11 | 0x0b | 0.00E+00 |
12 | 0x0c | # ?/? |
13 | 0x0d | # ??/?? |
14 | 0x0e | m/d/yy |
15 | 0x0f | d-mmm-yy |
16 | 0x10 | d-mmm |
17 | 0x11 | mmm-yy |
18 | 0x12 | h:mm AM/PM |
19 | 0x13 | h:mm:ss AM/PM |
20 | 0x14 | h:mm |
21 | 0x15 | h:mm:ss |
22 | 0x16 | m/d/yy h:mm |
... | ... | ... |
37 | 0x25 | (#,##0_);(#,##0) |
38 | 0x26 | (#,##0_);[Red](#,##0) |
39 | 0x27 | (#,##0.00_);(#,##0.00) |
40 | 0x28 | (#,##0.00_);[Red](#,##0.00) |
41 | 0x29 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
42 | 0x2a | _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_) |
43 | 0x2b | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
44 | 0x2c | _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_) |
45 | 0x2d | mm:ss |
46 | 0x2e | [h]:mm:ss |
47 | 0x2f | mm:ss.0 |
48 | 0x30 | ##0.0E+0 |
49 | 0x31 | @ |
void format_set_unlocked | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
This property can be used to allow modification of a cell in a protected worksheet. In Excel, cell locking is turned on by default for all cells. However, it only has an effect if the worksheet has been protected using the worksheet worksheet_protect() function:
void format_set_hidden | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
This property is used to hide a formula while still displaying its result. This is generally used to hide complex calculations from end users who are only interested in the result. It only has an effect if the worksheet has been protected using the worksheet worksheet_protect() function:
void format_set_align | ( | lxw_format * | format, |
uint8_t | alignment | ||
) |
format | Pointer to a Format instance. |
alignment | The horizontal and or vertical alignment direction. |
This method is used to set the horizontal and vertical text alignment within a cell. The following are the available horizontal alignments:
The following are the available vertical alignments:
As in Excel, vertical and horizontal alignments can be combined:
Text can be aligned across two or more adjacent cells using the center_across property. However, for genuine merged cells it is better to use the worksheet_merge_range() worksheet method.
The vertical justify option can be used to provide automatic text wrapping in a cell. The height of the cell will be adjusted to accommodate the wrapped text. To specify where the text wraps use the format_set_text_wrap() method.
void format_set_text_wrap | ( | lxw_format * | format | ) |
Turn text wrapping on for text in a cell.
If you wish to control where the text is wrapped you can add newline characters to the string:
Excel will adjust the height of the row to accommodate the wrapped text. A similar effect can be obtained without newlines using the format_set_align() function with LXW_ALIGN_VERTICAL_JUSTIFY.
void format_set_rotation | ( | lxw_format * | format, |
int16_t | angle | ||
) |
format | Pointer to a Format instance. |
angle | Rotation angle in the range -90 to 90 and 270. |
Set the rotation of the text in a cell. The rotation can be any angle in the range -90 to 90 degrees:
The angle 270 is also supported. This indicates text where the letters run from top to bottom.
void format_set_indent | ( | lxw_format * | format, |
uint8_t | level | ||
) |
format | Pointer to a Format instance. |
level | Indentation level. |
This method can be used to indent text in a cell. The argument, which should be an integer, is taken as the level of indentation:
void format_set_shrink | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
This method can be used to shrink text so that it fits in a cell:
void format_set_pattern | ( | lxw_format * | format, |
uint8_t | index | ||
) |
format | Pointer to a Format instance. |
index | Pattern index. |
Set the background pattern for a cell.
The most common pattern is a solid fill of the background color:
The available fill patterns are:
Fill Type | Define |
---|---|
Solid | LXW_PATTERN_SOLID |
Medium gray | LXW_PATTERN_MEDIUM_GRAY |
Dark gray | LXW_PATTERN_DARK_GRAY |
Light gray | LXW_PATTERN_LIGHT_GRAY |
Dark horizontal line | LXW_PATTERN_DARK_HORIZONTAL |
Dark vertical line | LXW_PATTERN_DARK_VERTICAL |
Dark diagonal stripe | LXW_PATTERN_DARK_DOWN |
Reverse dark diagonal stripe | LXW_PATTERN_DARK_UP |
Dark grid | LXW_PATTERN_DARK_GRID |
Dark trellis | LXW_PATTERN_DARK_TRELLIS |
Light horizontal line | LXW_PATTERN_LIGHT_HORIZONTAL |
Light vertical line | LXW_PATTERN_LIGHT_VERTICAL |
Light diagonal stripe | LXW_PATTERN_LIGHT_DOWN |
Reverse light diagonal stripe | LXW_PATTERN_LIGHT_UP |
Light grid | LXW_PATTERN_LIGHT_GRID |
Light trellis | LXW_PATTERN_LIGHT_TRELLIS |
12.5% gray | LXW_PATTERN_GRAY_125 |
6.25% gray | LXW_PATTERN_GRAY_0625 |
void format_set_bg_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell pattern background color. |
The format_set_bg_color() method can be used to set the background color of a pattern. Patterns are defined via the format_set_pattern() method. If a pattern hasn't been defined then a solid fill pattern is used as the default.
Here is an example of how to set up a solid fill in a cell:
The color should be an RGB integer value, see Working with Colors.
void format_set_fg_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell pattern foreground color. |
The format_set_fg_color() method can be used to set the foreground color of a pattern.
The color should be an RGB integer value, see Working with Colors.
void format_set_border | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Border style index. |
Set the cell border style:
Individual border elements can be configured using the following functions with the same parameters:
A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same value using format_set_border() or individually using the relevant method calls shown above.
The following border styles are available:
The most commonly used style is the thin
style.
void format_set_bottom | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Border style index. |
Set the cell bottom border style. See format_set_border() for details on the border styles.
void format_set_top | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Border style index. |
Set the cell top border style. See format_set_border() for details on the border styles.
void format_set_left | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Border style index. |
Set the cell left border style. See format_set_border() for details on the border styles.
void format_set_right | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | Border style index. |
Set the cell right border style. See format_set_border() for details on the border styles.
void format_set_border_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell border color. |
Individual border elements can be configured using the following methods with the same parameters:
Set the color of the cell borders. A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same color using format_set_border_color() or individually using the relevant method calls shown above.
The color should be an RGB integer value, see Working with Colors.
void format_set_bottom_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell border color. |
See format_set_border_color() for details on the border colors.
void format_set_top_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell border color. |
See format_set_border_color() for details on the border colors.
void format_set_left_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell border color. |
See format_set_border_color() for details on the border colors.
void format_set_right_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell border color. |
See format_set_border_color() for details on the border colors.
void format_set_diag_type | ( | lxw_format * | format, |
uint8_t | type | ||
) |
format | Pointer to a Format instance. |
type | The lxw_format_diagonal_types diagonal border type. |
Set the diagonal cell border type:
The allowable border types are defined in lxw_format_diagonal_types:
If the border style isn't specified with format_set_diag_border()
then it will default to LXW_BORDER_THIN.
void format_set_diag_border | ( | lxw_format * | format, |
uint8_t | style | ||
) |
format | Pointer to a Format instance. |
style | The lxw_format_borders style. |
Set the diagonal border style. This should be a lxw_format_borders value. See the example above.
void format_set_diag_color | ( | lxw_format * | format, |
lxw_color_t | color | ||
) |
format | Pointer to a Format instance. |
color | The cell diagonal border color. |
Set the diagonal border color. The color should be an RGB integer value, see Working with Colors and the above example.
void format_set_quote_prefix | ( | lxw_format * | format | ) |
format | Pointer to a Format instance. |
Set the quote prefix property of a format to ensure a string is treated as a string after editing. This is the same as prefixing the string with a single quote in Excel. You don't need to add the quote to the string but you do need to add the format.
typedef uint32_t lxw_color_t |
The type for RGB colors in libxlsxwriter. The valid range is 0x000000
(black) to 0xFFFFFF
(white). See Working with Colors.
typedef struct lxw_format lxw_format |
Formats in libxlsxwriter
are accessed via this struct.
The members of the lxw_format struct aren't modified directly. Instead the format properties are set by calling the functions shown in format.h.
For example:
Format underline values for format_set_underline().
enum lxw_format_scripts |
Superscript and subscript values for format_set_font_script().
Enumerator | |
---|---|
LXW_FONT_SUPERSCRIPT | Superscript font |
LXW_FONT_SUBSCRIPT | Subscript font |
Alignment values for format_set_align().
enum lxw_defined_colors |
Predefined values for common colors.
enum lxw_format_patterns |
Pattern value for use with format_set_pattern().
enum lxw_format_borders |
Cell border styles for use with format_set_border().
Data Structures | |
struct | lxw_format |
Struct to represent the formatting properties of an Excel format. More... | |
Typedefs | |
typedef uint32_t | lxw_color_t |
The type for RGB colors in libxlsxwriter. | |
typedef struct lxw_format | lxw_format |
Struct to represent the formatting properties of an Excel format. | |