Images permanently inserted into python code

I have python code that generates excel from other files, and I want to permanently incorporate photos into that code. To enable anyone to open my programme and generate an excel, I want to create an.exe file. There should be two photos in the header of this file (excel). How do I get the software or application to load the right files from the user’s computer? Because of the username and the location of the file’s storage, the path to the file may change. I’m going to presume that both of the picture files and the.exe executable file are in the same folder.

Below is the code of how it works now:

    logo = Image("C:/Users/STJ2TW/Desktop/folder/logo.png")
    element = Image("C:/Users/STJ2TW/Desktop/folder/element.png")
    logo.width = 500
    logo.height = 120
    element.width = 140
    element.height = 140
    element.alignment = Alignment(horizontal="left", vertical="center")
    ws.add_image(logo, 'A1')
    ws.add_image(element, 'E1')

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
January 26, 2023

Hi @likith,

I’m not sure there is a way to know the exact path every time. What you can try though are two things in my eyes.

  1. Rather than specifying the full path in both your log and element variable, you can try:
    path = "C:/Users/STJ2TW/Desktop/folder"
    logo = Image(f"{path}/logo.png")
    element = Image(f"{path}/element.png")

The above would mean you need to configure only the path in the program. Since all people will have a different setup, you can just set instructions on how to configure it.

An alternative would be to set instructions for where the images need to be, in which directory.

  1. Create the PATH as an argument. Whenever someone is running your script, they would be required to specify the path. That way you can add any path you want as an argument.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel