r/learnpython 2d ago

Problem with an extra row in Excel on Python

I have a Python table, but there are extra cells at the top when I output it; I’d like to remove them—how can I do that?

Using Openpyxl.

0 Upvotes

7 comments sorted by

1

u/socal_nerdtastic 2d ago

We'd have to see your code to help with that.

2

u/FearawaitsTM 2d ago
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_paths = fd.askopenfilenames(
            filetypes=[
                (
                    'Excel файлы',
                    '*.xlsx'
                )
            ]
        )
        for file_path in file_paths:
            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()

2

u/ManzoorAhmedShaikh 2d ago

I didn't see you using openpyxl, instead pandas is being used here. Normally if your excel file contains no headers at the top, the statement:

df = pd.read_excel(file_path)

will serve the first row as header, so for that you can pass `skiprows = 1` parameter in the pd.read_excel method. That'll help resolving your problem but still, there are few other possibilities as well, depends on your file.

1

u/FearawaitsTM 2d ago

I don't quite understand how to properly integrate openpyxl yet, so I haven't used it; I want to figure out how to get rid of the extra rows at the top of the Excel file.

1

u/ManzoorAhmedShaikh 2d ago

Can you share the first 2-3 rows of your excel file? Also, did you try adding that "skiprows" parameter, probably it will resolve your issue. But if it does not, then try sharing excel file top 2-3 rows here so I can better guide you in detail.

0

u/FearawaitsTM 2d ago

You cannot post photos in this section.

1

u/socal_nerdtastic 2d ago

To skip the first row when loading the excel file change your load line to this:

df = pd.read_excel(file_path, skiprows=1)