2

So apparently, the tag doesn't even exist for pymsteams, so i really hope someone can help me figure this out. What i'm trying to do is write a python script to run a query and to send the results via MS Teams. I'm able to successfully send a text from the python script via myTeamsMessage.text("Hello World") but not the results of my variable df

I've tried finding videos on YouTube, and browsing the web, but there only seems to be examples of sending text. Perhaps someone may know of a workaround? Or even better, an actual solution?

import pymsteams
import pyodbc
import pandas as pd
import numpy as np

conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                      "Server=***-***-***.***.***.com;"
                      "Database=IA;"
                      "Trusted_Connection=no;"
                      "UID=***;"
                      "PWD=***")

query = "SELECT * FROM [IP].[dbo].[vFEP]"
df = pd.read_sql(query, conn)

myTeamsMessage = 
pymsteams.connectorcard("https://outlook.office.com/webhook/***")
myTeamsMessage.text("Hello World")
myTeamsMessage.send()


# def sleeve(df):
#    print(df)


# sleeve(df)

# myTeamsMessage = 
# pymsteams.connectorcard("https://outlook.office.com/webhook/***")
# myTeamsMessage.text(sleeve(df))
# myTeamsMessage.send()

This is the error i'm receiving.

"pymsteams.TeamsWebhookException: Summary or Text is required."

What i'm hoping to accomplish is to have the results of the df be sent via MS Teams. Again, i'm able to send a text via "", but that's all i'm limited to. Any help is greatly appreciated. Thank you!

idcny
  • 45
  • 1
  • 9
  • Your `sleeve` function uses `print` which prints the DataFrame to console but does *not* return a `str`. Your function returns `None`, and that's what you're trying to send as a message. It sounds like you really want to do something more like [this](https://stackoverflow.com/q/1218933/2144390). – Gord Thompson Aug 02 '19 at 17:30
  • Thanks for your response, unfortunately i'm unable to understand how to implement what they're talking about. I'm not too concerned about what's been commented out, since i was just experimenting, i just included it to show what i was trying. My main culprit is myTeamsMessage.text("Hello World") I can only use text in this function. What i really want to do is send the results within my df variable like this. myTeamsMessage.text(df) Similar to a print, but obviously it won't work. I'm sorry if what you referred me to actually is the answer, i just wasn't able to understand. I'm new. – idcny Aug 02 '19 at 18:24
  • How about `myTeamsMessage.text(df.to_string())` ...? – Gord Thompson Aug 02 '19 at 18:41
  • This worked, thank you so much! – idcny Aug 02 '19 at 18:50

2 Answers2

4

For a pandas DataFrame named df, print(df) will simply dump the DataFrame to the console. If you want to send a string representation of the DataFrame to a str variable instead of the console, use s = df.to_string().

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
1

You can also send Teams a text string containing HTML formatting to get formatted tables in the output message too.

Try changing your sleeve function to use the pandas to_html() function:

def sleeve(df):
    df.to_html()
jamdroid
  • 11
  • 1