Example: Unicode - Shift JIS — XlsxWriter Documentation

### Navigation

- index
- next |
- previous |
- XlsxWriter Documentation »
- Examples »

# Example: Unicode - Shift JIS

This program is an example of reading in data from a Shift JIS encoded text file and converting it to a worksheet.

The main trick is to ensure that the data read in is converted to UTF-8 within the Python program. The XlsxWriter module will then take take of writing the encoding to the Excel file.

The encoding of the input data shouldn’t matter once it can be converted to UTF-8 via the `codecs` module.

Code:

    ##############################################################################
    #
    # A simple example of converting some Unicode text to an Excel file using
    # the XlsxWriter Python module.
    #
    # This example generates a spreadsheet with some Japanese text from a file
    # with Shift-JIS encoded text.
    #
    # Copyright 2013, John McNamara, jmcnamara@cpan.org
    #
    import codecs
    from xlsxwriter.workbook import Workbook

    # Open the input file with the correct encoding.
    textfile = codecs.open('unicode_shift_jis.txt', 'r', 'shift_jis')

    # Create an new Excel file and convert the text data.
    workbook = Workbook('unicode_shift_jis.xlsx')
    worksheet = workbook.add_worksheet()

    # Widen the first column to make the text clearer.
    worksheet.set_column('A:A', 50)

    # Start from the first cell.
    row = 0
    col = 0

    # Read the text file and write it to the worksheet.
    for line in textfile:
        # Ignore the comments in the text file.
        if line.startswith('#'):
            continue

        # Write any other lines to the worksheet.
        worksheet.write(row, col, line.rstrip("\n"))
        row += 1

    workbook.close()

### Navigation

- index
- next |
- previous |
- XlsxWriter Documentation »
- Examples »