Last active
September 18, 2024 12:09
-
-
Save bandrel/dcde814f4c3f733eed5699bef0332bfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import openpyxl | |
import os | |
import sys | |
# Load the Excel file | |
file_path = sys.argv[1] | |
wb = openpyxl.load_workbook(file_path) | |
# Create a directory to save the text files | |
output_dir = 'excel_columns_text' | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
# Iterate through each sheet in the workbook | |
for sheet in wb.sheetnames: | |
ws = wb[sheet] | |
# Iterate through each column in the sheet | |
for col_num, col in enumerate(ws.iter_cols(values_only=True), start=1): | |
# Create a file for each column | |
col_file_name = f"{sheet}_col_{col_num}.txt" | |
col_file_path = os.path.join(output_dir, col_file_name) | |
with open(col_file_path, 'w') as col_file: | |
# Join the column values with newlines and write to file | |
col_data = '\n'.join([str(cell) if cell is not None else '' for cell in col]) | |
col_file.write(col_data) | |
print(f"Columns exported to text files in '{output_dir}' directory.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment