1

I am a student at a community college studying computer science, I consider myself to be a beginner at programming, I have experience with java and python doing basic scripts.

The point is, that a family member of mine ( my cousin) who has a bachelors in computers science is helping me out by challenging me, he challenged to create a python script that you can use from the command line to parse and graph excel files, so what he said is that I should be able to search the excel file, and then have it displays some kind of graph, he didn't specify what kind of graph, maybe as long as it shows something, he said that I can use anything in my power to complete this little project.

So my question is, how can I go about this? I seems very doable, I just dont know how to go about the search part and the graph part. I will appreciate it a lot of if you guys helped me out, thanks. (pardon my eglish grammar nazis)

2
  • Did you know how to parse and graph the excel file? Commented Feb 25, 2020 at 6:16
  • 1
    It seems CS 101 to me. I recommend you to check some cs courses publicly available, and get the idea of cs at least, and then go for Python 101. Commented Feb 25, 2020 at 6:41

1 Answer 1

3

Since your question has a very broad spectrum and you have mentioned that you are a beginner, I will make a few assumptions here to make this easier.

Assumptions

  • You have multiple excel files (file.xlsx) of the same format in the current folder.

  • You want to type your filename or filenames in the terminal and get their corresponding plots.

Workflow

  • Use python's built-in argparse module to collect the file names from the terminal

  • Use pandas and xlrd module to load the excel files

  • Use matplotlib to plot and show the plots.

Now let's say your current folder has the following xlsx files named tmp1.xlsx and tmp2.xlsx. The files have the same format and look like this:

enter image description here

Dependency Installation

Before diving into the code, make an environment and run the following commands to install the necessary dependencies:

pip install matplotlib pandas xlrd
sudo apt-get install python3-tk

Code

Now, make a python file named, excel_plot.py:

from argparse import ArgumentParser
import matplotlib.pyplot as plt
import pandas as pd


def get_filepaths():
    """Collect excel file paths from the terminal."""

    parser = ArgumentParser(description="Plot excel files")
    parser.add_argument(
        "filepath",
        type=str,
        nargs="+",
        help="provide the full path of your excel files",
    )
    filepaths = parser.parse_args()
    return filepaths


def plot_excels(filepaths):
    """Load and plot one aspect of the excel files."""

    # load the excel files that we got from the provided arguments
    for path in filepaths.filepath:
        df = pd.read_excel(path)
        df.plot(kind="line", title="Per unit price & number of units")
        plt.show()


if __name__ == "__main__":
    filepaths = get_filepaths()
    plot_excels(filepaths)

To check if the script works, run the following command on your terminal:

python excel_plot.py tmp1.xlsx 

This should return

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.