Skip to content

Instantly share code, notes, and snippets.

@jacobrosenthal
Created February 22, 2016 20:29
Show Gist options
  • Save jacobrosenthal/99f506df5ca4cea7182a to your computer and use it in GitHub Desktop.
Save jacobrosenthal/99f506df5ca4cea7182a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#https://xlsxwriter.readthedocs.org/example_unicode_polish_utf8.html
##############################################################################
#
# A simple example of converting some Unicode text to an Excel file using
# the XlsxWriter Python module.
#
# This example generates a spreadsheet with some Polish text from a file
# with UTF8 encoded text.
#
# Copyright 2013-2016, John McNamara, jmcnamara@cpan.org
#
import codecs
import xlsxwriter
import sys
# Open the input file with the correct encoding.
textfile = codecs.open(sys.argv[1], 'r', 'utf-8')
outname = sys.argv[1].split('.')[0] + '.xlsx'
print outname
# Create an new Excel file and convert the text data.
workbook = xlsxwriter.Workbook(outname)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment