title: writing xlsx file from csv in python
body: <p>This is my code which loads the spreadsheet from doc excel file and creates a csvTranslations.csv, translations.py and translations.xlsx. The problem is it is creating a blank translattions.xlsx file and I don't know what am i doing wrong.</p>
  
  <pre><code>import os
  import random
  import re
  import csv
  import requests
  import io
  import sys
  import copy
  import xlrd
  import codecs
  import time
  import xlrd
  from collections import OrderedDict
  import simplejson as json
  import xlsxwriter
  from pandas.io.excel import ExcelWriter
  import pandas
  import openpyxl
  from openpyxl import Workbook
  from openpyxl.utils  import get_column_letter
  from openpyxl import load_workbook
  from openpyxl.utils import get_column_letter
  </code></pre>
  
  <blockquote>
    <p>the function which will be called</p>
  </blockquote>
  
  <pre><code>def gen_translations():
      try:
          import xlrd
      except:
          print "Missing xlrd module!"
          return
      #return
      csv_file = 'translations.xlsx'
      if not os.path.exists(csv_file):
          print "Unable to update translations due to missing csv file!"
          return
  
      print "updating translations..."
      o = open("translations.py", "wb")
  </code></pre>
  
  <blockquote>
    <p>link to the google doc    </p>
  </blockquote>
  
  <pre><code>url = "https://docs.google.com/spreadsheets/d/1g3nk_yzQxQFSxPN2lCvWwiHH8MgAqhpu5S763FRD_5I/gviz/tq?tqx=out:csv&amp;sheet=translations"
      r = requests.get(url, verify=False)
  
      ow = io.open("csvTranslations.csv", "wb")
  
      ow.write(r.content)
  
      ow.close()
  
  
  
      try:
          print &gt;&gt; o, header
      except:
          print &gt;&gt; o, "#CANNOT PRINT HEADER:%s" % repr(header)
  
      with open("csvTranslations.csv", "rb") as csvfile:
          docreader = csv.reader(csvfile)
          headers = next(docreader)
          for row in docreader:
              decoded_row = [entry.decode("utf8") for entry in row]
              decoded_row[6] = translate(decoded_row[2], to_X)
  
              try:
                  id = int(decoded_row[0])
              except:
                  break
              keyword = decoded_row[1]
              languages = decoded_row[2:]
              '''
              try:
                  print u" ; ".join(languages)
              except:
                  print "#CANNOT PRINT:%s"%repr(languages)
              '''
              try:
                  print &gt;&gt; o, "    %s = Word(%d, %s)" % (keyword, id, languages)
              except:
                  print &gt;&gt; "ERROR: CANNOT PRINT %s = Word(%d,%s)" % (repr(keyword), id, repr(languages))
          try:
              print &gt;&gt; o, footer
          except:
              print &gt;&gt; o, "#ERROR CANNOT PRINT FOOTER:%s" % repr(footer)
          o.close()
          print "done!"
  </code></pre>
  
  <blockquote>
    <p>it seems that the problem is here because both csvTranslations and translations.py are being written but the translations.xlsx is not    </p>
  </blockquote>
  
  <pre><code>        csv.register_dialect('colons', delimiter=',')
  
          reader = csv.reader(csvfile, dialect='colons')
  
          wb = Workbook()
          dest_filename = r"translations.xlsx"
  
          ws = wb.worksheets[0]
  
          for row_index, row in enumerate(reader):
              for column_index, cell in enumerate(row):
                  column_letter = get_column_letter((column_index + 1))
                  ws['%s%s'%(column_letter, (row_index + 1))].value = cell
  
          wb.save(filename=dest_filename)
  </code></pre>