Python Forum

Full Version: Convert Excel file to Text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Community:

I am a new Python student trying to learn more about this incredible programming language. I hope you could help me teach how to convert Excel file (.xlsx) to Text file (.txt) using a Python code.

This is not a high priority request, so please take as much time as you need. Your help will make my daily job much easier.

If you have any questions, please respond to this thread.
what have you tried so far?
We're glad to help, but won't write it for you without an effort on your part.
I see that. Thanks.

Here is the code:

import pandas as pd

df = pd.read_excel('C:\\Users\\path\\filename.xlsx', sheet_name='Sheet1', index=0)
with open('C:\\path\\filename.txt') as outfile:
	pd.to_string(outfile)
I am getting this error as a result:

Quote:Traceback (most recent call last):
File "C:\Users\file\filename.py", line 5, in <module>
pd.to_string(outfile)
File "C:\Users\file\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\__init__.py", line 263, in __getattr__
raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'to_string'
[Finished in 0.9s]
pd is what you called pandas, your dataframe that you want to store as text is called df so you need df.to_string().

import pandas as pd
 
df = pd.read_excel('C:\\Users\\path\\filename.xlsx', sheet_name='Sheet1', index=0)
with open('C:\\path\\filename.txt', 'w') as outfile:
    df.to_string(outfile)
This will create a text file but it will also have the pandas index numbers and the column headers. You can turn those off.

Since you only said excel to text we can't really tell what you need. Excel has lots of metadata for formatting, multiple sheets, etc. Converting it to pure text would give you one long string with all the data in the cells but no format or tabular separators (commas, tabs, spaces, linefeeds, etc.) so your columns and rows would be lost. That's probably NOT what you want so let us know more specifics if you can't figure it out by reading some docs...

EDIT: I also added the write flag ('w') to the open file. It might default to that but being explicit is always a good idea in python code.
Hi @Marbelous,

Thank you very much for your assistance! I highly appreciate that! Thumbs Up

Your Python code was successful at converting an Excel file to text. Here is the text output I am getting when the code is run:

Output:
E-mail Address First Name Last Name 0 [email protected] John Smith 1 [email protected] Jane Anderson
However, I am looking for the following output:

Output:
E-mail Address First Name Last Name [email protected] John Smith [email protected] Jane Anderson
Is there a way to change the code to get the output I am looking for?

My excel file has only one sheet labelled as Page 1 and the data looks like this:

Output:
Column 1 Column 2 Column 3 Row 1 E-mail Address First Name Last Name Row 2 [email protected] John Smith Row 3 [email protected] Jane Anderson
Please let me know if you have any questions.
The extra column is the pandas indexes I mentioned. You can turn it off with the index attribute of the df.to_string() method:

df.to_string(outfile, index=False)
Thank you very much for your assistance, Marbelous!