-1

I'm trying to use Python to convert an .xlsx file into a .txt file, but the resulting file is corrupt. When I manually open the .xlsx file and use File>Save as>.txt the file is fine. How can I convert the file with Python without corrupting it?

import openpyxl
wb = openpyxl.load_workbook('/Users/name/Desktop/Template.xlsx')
wb.save('/Users/name/Desktop/Template.upd.txt')
Kate
  • 47
  • 1
  • 4
  • 1
    Nothing is "corrupt", but openpyxl doesn't export text files. Giving the output file a ".txt" extension doesn't accomplish anything; it still writes an Excel file, not a text file. – hobbs Jan 11 '20 at 04:28
  • what delimiter is being used in xlsx – WhySoSerious Jan 11 '20 at 04:31

1 Answers1

0

You can write the sheet in a csv, but use tab as delimeter. With pandas this should work. More details on these functions are in here and here

import pandas as pd

df = pd.read_excel("yourexcel.xlsx",sheet_name="yoursheet",header=0)
df.to_csv("yourtxt.txt",sep="\t")
Atreyagaurav
  • 1,145
  • 6
  • 15