Using openpyxl with Uplink

Hi,
I have tried to replicate the code above with FileLoader component in Anvil using Uplink to Load the uploaded spreadsheet but with openpyxl in Pycharm and i am having the error:
InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
There is any possibility to load the uploaded the excel file from Anvil that can be readable by openpyxl.
Thanks for any help you can provide.

Hi @besarberdica and welcome to the forum!

I’ve moved your comment to a new thread since the old thread was from a few years ago, and it seems like your issue is with using openpyxl.

If you could post a few snippets of your code, it will be easier to try and pinpoint what’s going on.

I am using FileLoader in Anvil to import an Excel file. This command is linked with Pycharm locally using Uplink. After uploading the Excel file(I don’t want to save it) I want to create a temporary file with a unique name and read this Excel with openpyxl. I want to work locally because of the following code script commands.
In python i wrote this code:

import anvil.server
import random

@anvil.server.callable
def load_excel(file):

tmp_name = "/Temp/%s" % "".join([random.choice("0123456789abcdef") for x in range(32)])
with open(tmp_name, "wb") as f:
    f.write(file.get_bytes())
import openpyxl
wk = openpyxl.load_workbook(tmp_name, data_only = True)
sh1 = wk.active
print(sh1)

anvil.server.connect(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”)

anvil.server.wait_forever()

the error message is

InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm

I understand that creating a temporary file is not supported by openpyxl.
How can I do this process by not changing the Excel format, just upload the Excel file(not saving it) directly from Anvil(FileLoader) to python and read it with openpyxl.

Thank you in advance

If I remember correctly, it is possible for a server-side function to create and use a temporary file. Once the outermost server function returns to its client/uplink caller, the file will probably be deleted.

As there have been numerous posts on this topic, this would be a good time to use Search.

1 Like

Hi, I tried also the server module but with the same error that openpyxl does not support tmp file.
Maybe I did not explained better on what I looking for.

Below a part of the code that I use in my pc with tkinter.
.
.

from tkinter import filedialog
def fileDialog():
      filename = filedialog.askopenfilename(initialdir = “/”, title = “Select a File”,
      filetype = ((“jpeg”, “ *.jpg"), (“All Files”, "* .*”)))
      label1.configure(text = filename)
      import openpyxl
      wk = openpyxl.load_workbook(filename, data_only = True)
      sh1 = wk.active
      print(sh1)

btn1 = Button(MainFrame, pady = 10, bd = 4, font = (‘font’, 15, ‘normal’), width = 35,
text = “Upload file”, command = fileDialog)

My question is if using FileLoader button (from Anvil) can I load the workbook to my pc regardless the filename using Uplink in order to read it with openpyxl in my pc?
Thanks

It’s my fault for not reading more carefully.

FileLoader is for uploading files (sending files in the opposite direction: from the PC to Anvil).

What you’re looking for is probably the download function. See Uploading and downloading files for more details.

Edit: That would be for code running in the browser. If the code that needs the file is running entirely in an Uplink module, then a more mechanical approach should work. Your file (as a MediaObject) has a get_bytes function. You could write these bytes to disk as a file, with any name you want, using Python’s built-in file routines.

Thanks for your advise, I have found the solution I was searching. Its not your fault but since I am a beginner in this field I have difficulties to understand or to express what I am looking for.