r/learnpython 2d ago

Help with Excel cells in Python

Could you please advise me on exactly what I should study to be able to open two or more Excel files and select specific cells? For instance, I want to take cells A2 through B2 from the first file, while C2 in Python will be the result of A2 multiplied by B2. Here is the key point to understand: in Python, I want to treat the Excel cell D2 as D1 (because of the offset in Excel); essentially, I want to continue working with that first file but map the Excel column C2 to position D1 in Python, and do the same for the second file. I know I’ll need to create a list, but right now it’s hard for me to even articulate the idea clearly. Thanks everyone for your attention!

Here is my code; ideally, I’d like something created based on it.

Please also let me know if you have any issues with the code.

import pandas as pd

from pandastable import Table, TableModel

import tkinter as tk

from tkinter import ttk
from tkinter import filedialog,messagebox

fd = filedialog

class ExcelViewer:
    def __init__(self, root):
        self.root = root
        self.root.title = ("Чтение ячеек Excel")
        self.root.geometry("800x600")

        self.btn_load = tk.Button(root, text="Открыть Excel файл", command=self.open_file)
        self.btn_load.pack()

        self.result_label = tk.Label(root, text="Выбрать файл для начала")
        self.result_label.pack()

        self.frame = tk.Frame(root)
        self.frame.pack(fill='both', expand=True)

        self.table = None

        self.scrollbar = ttk.Scrollbar(root)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)


    def open_file(self):
        file_path = fd.askopenfilenames(
            filetypes=[
                (
                    'Excel файлы',
                    '*.xlsx'
                )
            ]
        )

        if not file_path:
            return

        if file_path:
            try:
                df = pd.read_excel(file_path)

                if self.table:
                    self.table.destroy()

                self.model = TableModel()
                self.model.df = df
                self.table = Table(self.frame, model = self.model, showtoolbar=False, showstatusbar=False)
                self.table.show()

            except Exception as e:
                tk.messagebox.showerror("Ошибка", f"Не удалось открыть файл:\n{e}")

if __name__ == "__main__":
    root = tk.Tk()
    app = ExcelViewer(root)
    root.mainloop()
9 Upvotes

3 comments sorted by

2

u/karpomalice 2d ago

first, if you can't articulate the idea then it will be difficult for you or anyone else to properly develop a solution.

openpyxl is going to give you syntax closer to excel terminology. But you can also just use pandas as a tool.

Either way you'll need to learn one of those two libraries to accomplish excel file manipulation.

2

u/argh1989 2d ago

You've described how you want to solve your problem but no what the problem is, which makes this confusing.

Maybe the Cyrillic in your code make it clearer but it's unclear to me what the data in these two sheets is of why you would want to use python to do multiplication for Excel instead of doing the whole thing in Excel.