import re


def sanitize_worksheet_name(
    name,
    max_width = 31):
    """
    Sanitizes XLSX worksheet name.

    See https://support.office.com/en-us/article/Rename-a-worksheet-3F1F7148-EE83-404D-8EF0-9FF99FBAD1F9.
    """

    if name.startswith('\''):
        name = name[1:]

    name = re.sub(
        r'\0|\*|/|:|\?|\[|\\|\]', '', name)

    name = name[:max_width]

    if name.endswith('\''):
        name = name[:-1]

    if name == 'History':
        name = 'History_'

    return name