title: Convert .xlsx to .txt with python? or format .txt file to fix columns indentation?
body: <p>I have an excel file with many rows/columns and when I convert the file directly from .xlsx to .txt with excel, the file ends up with a weird indentation (the columns are not perfectly aligned like in an excel file) and due to some requirements, I really need them to be. </p>
  
  <p>So, is there a better way to write from excel to txt using python? or format the txt file so the columns perfectly align?</p>
  
  <p>I found this code in a previous question but I am getting the following error:</p>
  
  <pre><code>TypeError: a bytes-like object is required, not 'str'
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>import xlrd
  import csv
  
  # open the output csv
  with open('my.csv', 'wb') as myCsvfile:
      # define a writer
      wr = csv.writer(myCsvfile, delimiter="\t")
  
      # open the xlsx file 
      myfile = xlrd.open_workbook('myfile.xlsx')
      # get a sheet
      mysheet = myfile.sheet_by_index(0)
  
      # write the rows
      for rownum in range(mysheet.nrows):
          wr.writerow(mysheet.row_values(rownum))
  </code></pre>