Skip to content

Instantly share code, notes, and snippets.

@bandrel
Last active September 18, 2024 12:09
Show Gist options
  • Save bandrel/dcde814f4c3f733eed5699bef0332bfc to your computer and use it in GitHub Desktop.
Save bandrel/dcde814f4c3f733eed5699bef0332bfc to your computer and use it in GitHub Desktop.
#!/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