1

I'm wondering if it's possible for me to show a progress bar (or anything like that) of the workbook I'm loading, so the user can know how long it would take.

I'm using openpyxl to load the workbook.

Code sample:

from openpyxl import load_workbook

sheet = 'my_sheet.xlsl'

# I want to load the workbook, as below, but I would like to
# show some loading progress info like a bar or %
my_workbook = load_workbook(sheet)
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Breno
  • 11
  • 2

1 Answers1

1

If you know in advance how many bytes you are going to download (and I assume you do since you know the file size), the simplest thing to do is set the maxvalue option to the number you are going to read. Then, each time you read a chunk, you configure the value to be the total number of bytes read. The progress bar will then figure out the percentage.

Here's a simulation to give you a rough idea:

import tkinter as tk
from tkinter import ttk


class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.button = ttk.Button(text="start", command=self.start)
        self.button.pack()
        self.progress = ttk.Progressbar(self, orient="horizontal",
                                        length=200, mode="determinate")
        self.progress.pack()

        self.bytes = 0
        self.maxbytes = 0

    def start(self):
        self.progress["value"] = 0
        self.maxbytes = 50000
        self.progress["maximum"] = 50000
        self.read_bytes()

    def read_bytes(self):
        '''simulate reading 500 bytes; update progress bar'''
        self.bytes += 500
        self.progress["value"] = self.bytes
        if self.bytes < self.maxbytes:
            # read more bytes after 100 ms
            self.after(100, self.read_bytes)

app = SampleApp()
app.mainloop()

For this to work you're going to need to make sure you don't block the GUI thread. That means either you read in chunks (like in the example) or do the reading in a separate thread. If you use threads you will not be able to directly call the progressbar methods because tkinter is single threaded.

You might find the progressbar example on tkdocs.com to be useful.

Credit to this post.

APhillips
  • 1,175
  • 9
  • 17