Images dissapear in excel documents when copying them with python

后端 未结 3 1903
南方客
南方客 2021-01-13 11:26
import openpyxl

def factuur():
    wb = openpyxl.load_workbook(\'factuurvoorbeeld1.xlsx\')
    Blad1 = wb.active
    Blad1[\'K7\'] = \'logo.png\'
    Blad1[\'E22\']         


        
相关标签:
3条回答
  • 2021-01-13 11:47

    Unfortunately openpyxl does not support images at this time, see official documentation: https://openpyxl.readthedocs.io/en/default/usage.html

    openpyxl does currently not read all possible items in an Excel file so images and charts will be lost from existing files if they are opened and saved with the same name.

    So when you open the template file, the images are not read, thus not saved in the new file.

    It might work if you insert the image again with openpyxl, by following the doc here: https://openpyxl.readthedocs.io/en/default/usage.html#inserting-an-image

    0 讨论(0)
  • 2021-01-13 11:48

    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()
    
    0 讨论(0)
  • 2021-01-13 11:57

    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()
    
    0 讨论(0)
提交回复
热议问题