-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-xlxs2txt.py
More file actions
33 lines (29 loc) · 1.07 KB
/
convert-xlxs2txt.py
File metadata and controls
33 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import sys
import pandas as pd
from pathlib import Path
# Set the input and output paths
INPUT_PATH = sys.argv[1]
OUTPUT_PATH = sys.argv[2]
def convert_xlsx_to_txt(input_file, output_file):
"""
Converts an XLSX file to a text file.
Args:
input_file (str): The path to the input XLSX file.
output_file (str): The path to the output text file.
"""
try:
df = pd.read_excel(input_file)
with open(output_file, 'w') as f:
df.to_string(f, index=False) # Write the DataFrame to the file as a string
print(f"Successfully converted '{input_file}' to '{output_file}'")
except FileNotFoundError:
print(f"Error: Input file '{input_file}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
#for filename in os.listdir(INPUT_PATH):
for filename in Path(INPUT_PATH).rglob("*.xlsx"):
# if filename.endswith(".xlsx"):
filepath = os.path.join(INPUT_PATH, filename)
output_file = os.path.join(OUTPUT_PATH, f"{filename}.txt")
convert_xlsx_to_txt(filepath, output_file)