Make pptx file from Python #chemoinformatics #memo #python-pptx

I hope all reader enjoying chemoinformatics ;)
Unfortunately I’m struggling to make PPTX file in this year. So I can’t have enought time to coding. But It’s same for medicinal chemists. Because they often make presentation slide for project team, theier boss, senior etc….

They transfer SAR data from spread sheet to pptx. Sometime it seems time consuming task for me.

So I would like to write code for making automatic pptx generator with python ;)

As you know there are useful prior art in the topic. The URL is listed below.

https://github.com/dkuhn/sdf2ppt
https://docs.eyesopen.com/toolkits/cookbook/python/depiction/csv2pptx.html?highlight=pptx

The first one uses RDKit and the second one uses OETK.

In this post, I used RDKit and most of code is bollowed from the OE’s example code.

Here is a my example code.
At first, import libraries which are required to make pptx and handling chemical structure.

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
from io import BytesIO, StringIO
from pptx import Presentation
from pptx.util import Inches
# read example molecules
mols = [m for m in Chem.SDMolSupplier('./cdk2.sdf')]
for m in mols:
    AllChem.Compute2DCoords(m)

Then define function to draw molecule and make pptx. I used BytesIO because by using the BytesIO I don’t need to make tempolary image file.

def getmol_bio(mol, size=(200, 200), fontsize=-1):
    bio = BytesIO()
    drawer = rdMolDraw2D.MolDraw2DCairo(size[0], size[1])
    op = drawer.drawOptions()
    op.fixedFontSize = fontsize
    drawer.DrawMolecule(mol)
    drawer.FinishDrawing()
    bio.write(drawer.GetDrawingText())
    return bio

def make_pptx(mol):
    pres = Presentation()
    title_slide_layout = pres.slide_layouts[0]
    title_slide = pres.slides.add_slide(title_slide_layout)
    title = title_slide.shapes.title
    title.text = 'mol props'
    slide_layout = pres.slide_layouts[5]
    slide = pres.slides.add_slide(slide_layout)
    bio = getmol_bio(mol, size=(300,200), fontsize=18)
    slide.shapes.add_picture(bio, left=Inches(1.0), top=Inches(2.0), width=Inches(2.5))
    props = mol.GetPropsAsDict()
    rows = len(props)
    cols = 2
    table = slide.shapes.add_table(rows, cols, left=Inches(4.0), top=Inches(2.0),
                                   width=Inches(5.5), height=Inches(0.8)).table
    table.columns[0].width = Inches(2.0)
    table.columns[1].width = Inches(3.5)
    table.first_row = False
    for row, (k,v) in enumerate(props.items()):
        table.cell(row, 0).text = k[:20]+':'
        table.cell(row, 1).text = str(v)
    pres.save('mol.pptx')

The main part of code is done. Let’s make PPTX file.

make_pptx(mols[0])

After calling make_pptx, I could get pptx file shown below.

It worked fine. This is a smile example but by using the approach it will be easy to make many compound related pptx files (IMHO, IMHO).

Python-pptx package is really cool and useful tool Ithink. It’s interesting for me! Love coding!

Published by iwatobipen

I'm medicinal chemist in mid size of pharmaceutical company. I love chemoinfo, cording, organic synthesis, my family.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: