Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images dissapear in excel documents when copying them with python

import openpyxl

def factuur():
    wb = openpyxl.load_workbook('factuurvoorbeeld1.xlsx')
    Blad1 = wb.active
    Blad1['K7'] = 'logo.png'
    Blad1['E22'] = factuurNr.get()
    Blad1['E23'] = datum.get()
    Blad1['E24'] = debNr.get()
    Blad1['E25'] = locatie.get()
    wb.save('{}.xlsx'.format(factuurNr.get() + ',' + debNr.get()))

I'm trying to program an application that automatically fills in Excel documents by using a simple GUI and entry fields. The problem is, everytime python makes a copy of the base Excel file (The one with personal information that is in every document along with one image), the image dissappears. Do any of you guys know what to do about this? I'm using Python 3.5.

Things I have tried: 1) multiple Excel modules 2) Something about writing code in excel(Not sure if I did this correctly though)

Sorry for my horrible description, I'm don't really know how to describe this kind of stuff. If you want to know more, just leave a question in the comments, I will check as often as i can.

Blad1 = The first sheet of the excel document.

like image 725
Ya boy Avatar asked Nov 15 '16 09:11

Ya boy


People also ask

Why do my images keep disappearing in Excel?

First,make sure you are using the latest version of Excel, After you copy the picture, right click>special paste>paste as picture and check the issue persist or not. If the problem continues, please ask whether the same problem occurs when you choose to insert a picture instead of pasting.

Can Python manipulate Excel files?

Excel is a popular and powerful spreadsheet application for Windows. The openpyxl module allows your Python programs to read and modify Excel spreadsheet files. For example, you might have the boring task of copying certain data from one spreadsheet and pasting it into another one.

How do I insert multiple images into Excel using Python?

How do I insert multiple pictures into an Excel spreadsheet? In the worksheet, click Insert > Picture. In the Insert Picture dialog, please open the folder containing pictures you will insert, select multiple pictures as you need, and click the Insert button.


2 Answers

I face same issue with openpyxl 2.6.2

Logo images was added in template excel file but disappear in exported .xlsx file

I just install Pillow and now its exporting all images fine.

pip install Pillow

This is my sample code to create salary slip from template excel file:

from openpyxl import load_workbook


def create_salary_slip():
    wb = load_workbook(filename = 'salary.xlsx')
    # grab the active worksheet
    ws = wb.active
    # Add Income
    ws['D11'] = 25000
    # Add deduction
    ws['M11'] = 1500
    # Save the file
    wb.save("sample.xlsx")

create_salary_slip()
like image 168
Rajiv Sharma Avatar answered Sep 19 '22 00:09

Rajiv Sharma


I had the same issue and tried a lot of libs. For me editpyxl does exactly what I need in that scenario:

import editpyxl

wb = editpyxl.Workbook()  
source_filename = r'Template.xlsx'
destination_filename = 'Output.xlsx'

wb.open(source_filename)
ws = wb.active
ws.cell('A1').value = 3.14
wb.save(destination_filename)
wb.close()
like image 31
Stefan Avatar answered Sep 19 '22 00:09

Stefan