21

Suppose I have a DataFrame I want to export to a PDF. In the DataFrame I have the following columns: Code, Name, Price, Net, Sales. Every row is a Product.

I want to add to every product in that DataFrame an image which i could get using BeautifulSoup. Is there some way to add the image to the DataFrame? Not the link, just the image of the product.

Being more specific i want something like this:

enter image description here

Code:

import pandas as pd
df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales')

#Suppose this are the links that contains the imagen i want to add to the DataFrame
images = ['Link 1','Link 2'] 
1
  • 1
    Check this - display url link of pic. Commented Nov 25, 2018 at 14:47

2 Answers 2

37

You'll probably have to play a bit around with width and height attributes, but this should get you started. Basically, you're just converting the image/links to html, then using the df.to_html to display those tags. Note, it won't show if you're working in an IDE like PyCharm, Spyder, but as you can see below with my output, works fine through jupyter notebooks

import pandas as pd
from IPython.core.display import display,HTML

df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales'])

# your images
images1 = ['https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825',
          'https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png'] 


images2 = ['https://static3.srcdn.com/wordpress/wp-content/uploads/2020/07/Quidditch.jpg?q=50&fit=crop&w=960&h=500&dpr=1.5',
           'https://specials-images.forbesimg.com/imageserve/5e160edc9318b800069388e8/960x0.jpg?fit=scale']

df['imageUrls'] = images1
df['otherImageUrls'] = images2


# convert your links to html tags 
def path_to_image_html(path):
    return '<img src="'+ path + '" width="60" >'

pd.set_option('display.max_colwidth', None)

image_cols = ['imageUrls', 'otherImageUrls']  #<- define which columns will be used to convert to html

# Create the dictionariy to be passed as formatters
format_dict = {}
for image_col in image_cols:
    format_dict[image_col] = path_to_image_html


display(HTML(df.to_html(escape=False ,formatters=format_dict)))

Output

Then you have some options of what to do there to go to pdf.

You could save as html

df.to_html('test_html.html', escape=False, formatters=format_dict)

then simply use and html to pdf converter here, or use a library such as pdfkit or WeasyPrint. I'm not entirely familiar with those (I only used one of them once a long time ago), but here's a good link

Good luck.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for this great answer, @chitown88, it was exactly what I needed. The code just needs a bit of updating. Change: from IPython.core.display import HTML into from IPython.core.display import display, HTML And: HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html))) into display(HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html)))). As shown here
@chitown88 the formatters is missing while saving to html file. df.to_html('test_html.html', escape=False, formatters=dict(image=path_to_image_html))
Good question. I’ll update this code when I get chance to sit down at my laptop in a few hours.
@rom, Ok I updated the code. The reason it didn't work for you is you need to use a dictionary for the formatters.
theres multiple ways to do this too. You simply could apply that function seperately to each of the columns, then wouldnt need to use formatters param
|
0

A few times in the past I needed the ability to render elements in dataframes. Because of that I developed the package pandas-render. I'm the developer of this package and want to share this alternative approach.

  1. Install the package with: pip install pandas-render

  2. Create the dataframe:

    from pandas_render import pandas as pd
    
    df = pd.DataFrame(
        [
            ['A231', 'Book', 5, 3, 150], 
            ['M441', 'Magic Staff', 10, 7, 200]
        ],
        columns=['Code', 'Name', 'Price', 'Net', 'Sales']
    )
    
    images_1 = ['https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825',
              'https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png'] 
    
    images_2 = ['https://static3.srcdn.com/wordpress/wp-content/uploads/2020/07/Quidditch.jpg?q=50&fit=crop&w=960&h=500&dpr=1.5',
               'https://specials-images.forbesimg.com/imageserve/5e160edc9318b800069388e8/960x0.jpg?fit=scale']
    
    df['imageUrls'] = images_1
    df['otherImageUrls'] = images_2
    
  3. Render the dataframe:

    df.render({
            "Code": "<pre>{{ content }}</pre>",
            "Name": "<strong>{{ content }}</strong>",
            "Price": "<p>Price: {{ Price }}</p><p>Net: {{ Net }}</p><p>Sales: {{ Sales }}</p>",
            "imageUrls": '<img src="{{ content }}" width="50px"/>',
            "otherImageUrls": '<img src="{{ content }}" width="100px"/>',
        },
        table_column_names=["Code", "Name", "Inventory", "Item Image", "Item Example"],
        filter_columns=True
    )
    

    enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.