[GH-ISSUE #37] Feature request: worksheet_write_rich_string() #32

Closed
opened 2026-05-05 11:26:41 -06:00 by gitea-mirror · 6 comments
Owner

Originally created by @jmcnamara on GitHub (Dec 10, 2015).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/37

Originally assigned to: @jmcnamara on GitHub.

Add worksheet_write_rich_string() function like the Python XlsxWriter Worksheet method write_rich_string().
See https://xlsxwriter.readthedocs.io/example_rich_strings.html

  • Difficulty: 5 (Easy 1 - 5 Hard)
  • Priority: 2 (High 1 - 5 Low)

Add +1 as a comment to vote for this feature and to get an update when it is implemented.

If you would like to make a donation to accelerate this feature you can do so via PayPal or contact me directly. Currently $0 of $500.

Originally created by @jmcnamara on GitHub (Dec 10, 2015). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/37 Originally assigned to: @jmcnamara on GitHub. Add `worksheet_write_rich_string()` function like the Python XlsxWriter Worksheet method `write_rich_string()`. See https://xlsxwriter.readthedocs.io/example_rich_strings.html - Difficulty: 5 (Easy 1 - 5 Hard) - Priority: 2 (High 1 - 5 Low) Add +1 as a comment to vote for this feature and to get an update when it is implemented. If you would like to make a donation to accelerate this feature you can do so via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ATE9EFWNF7PBJ) or contact me [directly](jmcnamara@cpan.org). Currently $0 of $500.
gitea-mirror 2026-05-05 11:26:41 -06:00
Author
Owner

@ghost commented on GitHub (Jul 18, 2018):

+1

<!-- gh-comment-id:405924595 --> @ghost commented on GitHub (Jul 18, 2018): +1
Author
Owner

@guyverthree commented on GitHub (Aug 1, 2018):

+1

<!-- gh-comment-id:409522956 --> @guyverthree commented on GitHub (Aug 1, 2018): +1
Author
Owner

@jmcnamara commented on GitHub (Aug 6, 2018):

Since I've had a few requests about this I've added a note about making donations to prioritize this feature.

<!-- gh-comment-id:410818377 --> @jmcnamara commented on GitHub (Aug 6, 2018): Since I've had a few requests about this I've added a note about making donations to prioritize this feature.
Author
Owner

@jmcnamara commented on GitHub (Sep 29, 2018):

There is now working code for this feature on the rich_strings branch.

It still needs parameter validation and documentation before it is merged up to master but the core API and functionality is complete. Here is an example from the branch:

/*
 * An example of using the libxlsxwriter library to write some "rich strings",
 * i.e., strings with multiple formats.
 *
 * Copyright 2014-2018, John McNamara, jmcnamara@cpan.org
 *
 */

#include "xlsxwriter.h"

int main() {

    lxw_workbook  *workbook  = workbook_new("rich_strings.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    /* Set up some formats to use. */
    lxw_format *bold = workbook_add_format(workbook);
    format_set_bold(bold);

    lxw_format *italic = workbook_add_format(workbook);
    format_set_italic(italic);

    lxw_format *red = workbook_add_format(workbook);
    format_set_font_color(red, LXW_COLOR_RED);

    lxw_format *blue = workbook_add_format(workbook);
    format_set_font_color(blue, LXW_COLOR_BLUE);

    lxw_format *center = workbook_add_format(workbook);
    format_set_align(center, LXW_ALIGN_CENTER);

    lxw_format *superscript = workbook_add_format(workbook);
    format_set_font_script(superscript, LXW_FONT_SUPERSCRIPT);

    /* Make the first column wide for clarity. */
    worksheet_set_column(worksheet, 0, 0, 30, NULL);


    /* Create and write some rich strings with multiple formats. */

    /* Example 1. Some bold and italic in the same string. */
    lxw_rich_string_tuple fragment11 = {.format = NULL,   .string = "This is "};
    lxw_rich_string_tuple fragment12 = {.format = bold,   .string = "bold"};
    lxw_rich_string_tuple fragment13 = {.format = NULL,   .string = " and this is "};
    lxw_rich_string_tuple fragment14 = {.format = italic, .string = "italic"};
    lxw_rich_string_tuple *rich_string1[] = {&fragment11, &fragment12,
                                             &fragment13, &fragment14, NULL};

    worksheet_write_rich_string(worksheet, CELL("A1"), rich_string1, NULL);


    /* Example 2. Some red and blue coloring in the same string. */
    lxw_rich_string_tuple fragment21 = {.format = NULL,   .string = "This is "};
    lxw_rich_string_tuple fragment22 = {.format = red,    .string = "red"};
    lxw_rich_string_tuple fragment23 = {.format = NULL,   .string = " and this is "};
    lxw_rich_string_tuple fragment24 = {.format = blue,   .string = "blue"};
    lxw_rich_string_tuple *rich_string2[] = {&fragment21, &fragment22,
                                             &fragment23, &fragment24, NULL};

    worksheet_write_rich_string(worksheet, CELL("A3"), rich_string2, NULL);


    /* Example 3. A rich string plus cell formatting. */
    lxw_rich_string_tuple fragment31 = {.format = NULL,   .string = "Some "};
    lxw_rich_string_tuple fragment32 = {.format = bold,   .string = "bold text"};
    lxw_rich_string_tuple fragment33 = {.format = NULL,   .string = " centered"};
    lxw_rich_string_tuple *rich_string3[] = {&fragment31, &fragment32,
                                             &fragment33, NULL};

    /* Note that is example also can a "center" cell format. */
    worksheet_write_rich_string(worksheet, CELL("A5"), rich_string3, center);


    /* Example 4. A math example with a superscript. */
    lxw_rich_string_tuple fragment41 = {.format = italic,      .string = "j =k"};
    lxw_rich_string_tuple fragment42 = {.format = superscript, .string = "(n-1)"};
    lxw_rich_string_tuple *rich_string4[] = {&fragment41, &fragment42, NULL};

    worksheet_write_rich_string(worksheet, CELL("A7"), rich_string4, center);


    workbook_close(workbook);

    return 0;
}

And the output:

screenshot

The key idea is to split the string into to fragments that are preceded by the format. If the format is NULL then default formatting is applied.

The API is a little clunky, i.e., having to create a lxw_rich_string_tuple ** with the fragments) but that is suitable for wrapping in small user defined functions suitable to the data.

If you want to try it in advance let me know how you get on. All the tests are ported and passing.

<!-- gh-comment-id:425633033 --> @jmcnamara commented on GitHub (Sep 29, 2018): There is now working code for this feature on the [rich_strings](https://github.com/jmcnamara/libxlsxwriter/tree/rich_strings) branch. It still needs parameter validation and documentation before it is merged up to master but the core API and functionality is complete. Here is an example from the branch: ```C /* * An example of using the libxlsxwriter library to write some "rich strings", * i.e., strings with multiple formats. * * Copyright 2014-2018, John McNamara, jmcnamara@cpan.org * */ #include "xlsxwriter.h" int main() { lxw_workbook *workbook = workbook_new("rich_strings.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); /* Set up some formats to use. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); lxw_format *italic = workbook_add_format(workbook); format_set_italic(italic); lxw_format *red = workbook_add_format(workbook); format_set_font_color(red, LXW_COLOR_RED); lxw_format *blue = workbook_add_format(workbook); format_set_font_color(blue, LXW_COLOR_BLUE); lxw_format *center = workbook_add_format(workbook); format_set_align(center, LXW_ALIGN_CENTER); lxw_format *superscript = workbook_add_format(workbook); format_set_font_script(superscript, LXW_FONT_SUPERSCRIPT); /* Make the first column wide for clarity. */ worksheet_set_column(worksheet, 0, 0, 30, NULL); /* Create and write some rich strings with multiple formats. */ /* Example 1. Some bold and italic in the same string. */ lxw_rich_string_tuple fragment11 = {.format = NULL, .string = "This is "}; lxw_rich_string_tuple fragment12 = {.format = bold, .string = "bold"}; lxw_rich_string_tuple fragment13 = {.format = NULL, .string = " and this is "}; lxw_rich_string_tuple fragment14 = {.format = italic, .string = "italic"}; lxw_rich_string_tuple *rich_string1[] = {&fragment11, &fragment12, &fragment13, &fragment14, NULL}; worksheet_write_rich_string(worksheet, CELL("A1"), rich_string1, NULL); /* Example 2. Some red and blue coloring in the same string. */ lxw_rich_string_tuple fragment21 = {.format = NULL, .string = "This is "}; lxw_rich_string_tuple fragment22 = {.format = red, .string = "red"}; lxw_rich_string_tuple fragment23 = {.format = NULL, .string = " and this is "}; lxw_rich_string_tuple fragment24 = {.format = blue, .string = "blue"}; lxw_rich_string_tuple *rich_string2[] = {&fragment21, &fragment22, &fragment23, &fragment24, NULL}; worksheet_write_rich_string(worksheet, CELL("A3"), rich_string2, NULL); /* Example 3. A rich string plus cell formatting. */ lxw_rich_string_tuple fragment31 = {.format = NULL, .string = "Some "}; lxw_rich_string_tuple fragment32 = {.format = bold, .string = "bold text"}; lxw_rich_string_tuple fragment33 = {.format = NULL, .string = " centered"}; lxw_rich_string_tuple *rich_string3[] = {&fragment31, &fragment32, &fragment33, NULL}; /* Note that is example also can a "center" cell format. */ worksheet_write_rich_string(worksheet, CELL("A5"), rich_string3, center); /* Example 4. A math example with a superscript. */ lxw_rich_string_tuple fragment41 = {.format = italic, .string = "j =k"}; lxw_rich_string_tuple fragment42 = {.format = superscript, .string = "(n-1)"}; lxw_rich_string_tuple *rich_string4[] = {&fragment41, &fragment42, NULL}; worksheet_write_rich_string(worksheet, CELL("A7"), rich_string4, center); workbook_close(workbook); return 0; } ``` And the output: ![screenshot](https://user-images.githubusercontent.com/94267/46244389-4736ee00-c3d6-11e8-9eca-b11d4e68b669.png) The key idea is to split the string into to fragments that are preceded by the format. If the format is `NULL` then default formatting is applied. The API is a little clunky, i.e., having to create a `lxw_rich_string_tuple **` with the fragments) but that is suitable for wrapping in small user defined functions suitable to the data. If you want to try it in advance let me know how you get on. All the tests are ported and passing.
Author
Owner
<!-- gh-comment-id:425757182 --> @jmcnamara commented on GitHub (Sep 30, 2018): Added docs: https://github.com/jmcnamara/libxlsxwriter/blob/rich_strings/include/xlsxwriter/worksheet.h#L1338
Author
Owner

@jmcnamara commented on GitHub (Oct 1, 2018):

Added in version 0.8.3: https://libxlsxwriter.github.io/changes.html

<!-- gh-comment-id:426083429 --> @jmcnamara commented on GitHub (Oct 1, 2018): Added in version 0.8.3: https://libxlsxwriter.github.io/changes.html
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#32
No description provided.