The project outline:

Write a program that performs the tasks of the previous program in reverse order: the program should open a spreadsheet and write the cells of column A into one text file, the cells of column B into another text file, and so on.

My solution:

# A program to read a spreadsheet and write it to a text file with one text file per column
# Usage: python spreadsheet_to_text.py "spreadsheet path" "folder to save .txt files"

import sys, openpyxl
from pathlib import Path
from openpyxl.utils import get_column_letter

def main(spreadsheet_path, save_folder):
    workbook = openpyxl.load_workbook(spreadsheet_path)
    sheet = workbook.active
    for column_index in range(1, sheet.max_column + 1):
        file_path = save_folder / f"Text for column {get_column_letter(column_index)}.txt"
        with open(file_path, "w", encoding="utf-8") as text_file:
            for row_index in range(1, sheet.max_row + 1):
                cell = sheet.cell(row=row_index, column=column_index)
                if not cell.value:
                    text_file.write(str(cell.value) + "\n")

if __name__ == "__main__":
    spreadsheet_path = Path(sys.argv[1]) # The location of the spreadsheet to open
    save_folder = Path(sys.argv[2]) # The path of the folder to save the .txt files in
    main(spreadsheet_path, save_folder)

I decided to copy the formulas themselves rather than the result because the outline doesn't specify and the chapter doesn't discuss it.

  • 1
    \$\begingroup\$ The description as quoted seems incomplete without knowing what the "previous program" is. But it seems that can be omitted, and you can simply give the requirements for this one. (It's hard to see how the order could be reversed - we can't write the data before we read it, can we?) \$\endgroup\$ Nov 10, 2022 at 12:57
  • 1
    \$\begingroup\$ The tasks of the previous program were defined as follows: Write a program to read in the contents of several text files (you can make the text files yourself) and insert those contents into a spreadsheet, with one line of text per row. The lines of the first text file will be in the cells of column A, the lines of the second text file will be in the cells of column B, and so on. \$\endgroup\$
    – AcK
    Nov 10, 2022 at 18:54

1 Answer 1


Use PEP8 to format your code. Two blank lines between functions, and two between imports and the first function (main).

In the main function, save_folder seems to be a Path object, but that's not obvious from reading the code until you use the / notation. Maybe use the type hints in the function definition.

def main(spreadsheet_path: Path, save_folder: Path):
    # Your code here

Your main function is crowded--add in blank lines (e.g. before the outermost for loop) to make it easier to read.

It's possible for things like the file open to fail, if, for example, the program does not have the necessary permissions--consider using a try-except block to handle such cases.

It's possible your main function could be simplified using pandas, unless you were not allowed.

The main code should check that you have 2 arguments passed to begin with, that the paths exist, and other such conditions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.