Search code examples
pythonexcelpandas

why pandas in python does not return all the rows from excel sheet?


this is my code and my excel sheet has 1000 rows and 22 columns of data. i want to convert excel to text file. but when i run this, i only get first and line 5 rows and columns of data? how to solve this issue

import pandas as pd
xlsFile = pd.ExcelFile('sample.xlsx')
totalPages= xlsFile.sheet_names
with open('output.txt','w') as textFile:
    for page in totalPages:
        dataFrame = xlsFile.parse(page)
        textFile.write("\nSheet name is:"+page+"\n")
        textFile.write(str(dataFrame))
        textFile.write('\n')

Solution

  • You could try something like this

    import pandas as pd
    
    with open('sample.xlsx', 'w') as file:
        pd.read_excel('output.txt').to_string(file, index=False)
    

    code pulled from: Convert .xlsx to .txt with python? or format .txt file to fix columns indentation?