Send Graphs in Outlook with Python











up vote
0
down vote

favorite












I wanted to create better automated reports by including visuals rather than just text. The specific way I wanted to do it was by referencing outlook.application via win32com.client.



I know the basics of sending an email, and I've tried reading through the VBA reference and the closest I found was a View Object, but what I was looking for was the ability to add pie charts, bar graphs etc.



I know through the GUI, once in an open message popped out from the application you can Insert > Chart and select the desired chart to load into the message.



Is there a way in the outlook API to be able to utilize these graphs/charts that are available in the Insert Chart window? Or is the API relatively limited in features available compared to the application when it comes to message styling/formatting?



Here's the bare-bones code for how the message is created/sent, I was wanting to add these visuals to the message body.



import win32com.client as win32

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)

mail.To = 'some@name'
mail.Subject = 'Subject'
mail.Body = 'Body'
# or
mail.HTMLBody = '<h2>HTML body</h2>'

mail.Send()




Update



I'm currently looking into it more, but I found this for vba that's supposed to do what I'm looking for. I've been messing around with chart objects a bit in excel as well. Here's the code I need to understand



Sub CopyAndPasteToMailBody()
Set mailApp = CreateObject("Outlook.Application")
Set mail = mailApp.CreateItem(olMailItem)
mail.Display
Set wEditor = mailApp.ActiveInspector.wordEditor
ActiveChart.ChartArea.Copy
wEditor.Application.Selection.Paste
End Sub




Here's some bare bones code I have that will create a chart object:



from win32com import client

excel = client.Dispatch("excel.application")
excel.Visible = True
wb = excel.Workbooks.Add()
ws = wb.Worksheets.Add()
chs = ws.ChartObjects()
co = chs.Add(0,0,500,500)




Update



Below I have answered how to spawn a chart in Excel and have it sent through Outlook. It is not the 'Outlook API', as it's Office as a whole that must be utilized. If you were confused like I was by a lack of documentation for using win32com to interact with Office applications, that's because it's all here in the VBA Reference and just needs to be converted into python accordingly.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I wanted to create better automated reports by including visuals rather than just text. The specific way I wanted to do it was by referencing outlook.application via win32com.client.



    I know the basics of sending an email, and I've tried reading through the VBA reference and the closest I found was a View Object, but what I was looking for was the ability to add pie charts, bar graphs etc.



    I know through the GUI, once in an open message popped out from the application you can Insert > Chart and select the desired chart to load into the message.



    Is there a way in the outlook API to be able to utilize these graphs/charts that are available in the Insert Chart window? Or is the API relatively limited in features available compared to the application when it comes to message styling/formatting?



    Here's the bare-bones code for how the message is created/sent, I was wanting to add these visuals to the message body.



    import win32com.client as win32

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)

    mail.To = 'some@name'
    mail.Subject = 'Subject'
    mail.Body = 'Body'
    # or
    mail.HTMLBody = '<h2>HTML body</h2>'

    mail.Send()




    Update



    I'm currently looking into it more, but I found this for vba that's supposed to do what I'm looking for. I've been messing around with chart objects a bit in excel as well. Here's the code I need to understand



    Sub CopyAndPasteToMailBody()
    Set mailApp = CreateObject("Outlook.Application")
    Set mail = mailApp.CreateItem(olMailItem)
    mail.Display
    Set wEditor = mailApp.ActiveInspector.wordEditor
    ActiveChart.ChartArea.Copy
    wEditor.Application.Selection.Paste
    End Sub




    Here's some bare bones code I have that will create a chart object:



    from win32com import client

    excel = client.Dispatch("excel.application")
    excel.Visible = True
    wb = excel.Workbooks.Add()
    ws = wb.Worksheets.Add()
    chs = ws.ChartObjects()
    co = chs.Add(0,0,500,500)




    Update



    Below I have answered how to spawn a chart in Excel and have it sent through Outlook. It is not the 'Outlook API', as it's Office as a whole that must be utilized. If you were confused like I was by a lack of documentation for using win32com to interact with Office applications, that's because it's all here in the VBA Reference and just needs to be converted into python accordingly.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wanted to create better automated reports by including visuals rather than just text. The specific way I wanted to do it was by referencing outlook.application via win32com.client.



      I know the basics of sending an email, and I've tried reading through the VBA reference and the closest I found was a View Object, but what I was looking for was the ability to add pie charts, bar graphs etc.



      I know through the GUI, once in an open message popped out from the application you can Insert > Chart and select the desired chart to load into the message.



      Is there a way in the outlook API to be able to utilize these graphs/charts that are available in the Insert Chart window? Or is the API relatively limited in features available compared to the application when it comes to message styling/formatting?



      Here's the bare-bones code for how the message is created/sent, I was wanting to add these visuals to the message body.



      import win32com.client as win32

      outlook = win32.Dispatch('outlook.application')
      mail = outlook.CreateItem(0)

      mail.To = 'some@name'
      mail.Subject = 'Subject'
      mail.Body = 'Body'
      # or
      mail.HTMLBody = '<h2>HTML body</h2>'

      mail.Send()




      Update



      I'm currently looking into it more, but I found this for vba that's supposed to do what I'm looking for. I've been messing around with chart objects a bit in excel as well. Here's the code I need to understand



      Sub CopyAndPasteToMailBody()
      Set mailApp = CreateObject("Outlook.Application")
      Set mail = mailApp.CreateItem(olMailItem)
      mail.Display
      Set wEditor = mailApp.ActiveInspector.wordEditor
      ActiveChart.ChartArea.Copy
      wEditor.Application.Selection.Paste
      End Sub




      Here's some bare bones code I have that will create a chart object:



      from win32com import client

      excel = client.Dispatch("excel.application")
      excel.Visible = True
      wb = excel.Workbooks.Add()
      ws = wb.Worksheets.Add()
      chs = ws.ChartObjects()
      co = chs.Add(0,0,500,500)




      Update



      Below I have answered how to spawn a chart in Excel and have it sent through Outlook. It is not the 'Outlook API', as it's Office as a whole that must be utilized. If you were confused like I was by a lack of documentation for using win32com to interact with Office applications, that's because it's all here in the VBA Reference and just needs to be converted into python accordingly.










      share|improve this question















      I wanted to create better automated reports by including visuals rather than just text. The specific way I wanted to do it was by referencing outlook.application via win32com.client.



      I know the basics of sending an email, and I've tried reading through the VBA reference and the closest I found was a View Object, but what I was looking for was the ability to add pie charts, bar graphs etc.



      I know through the GUI, once in an open message popped out from the application you can Insert > Chart and select the desired chart to load into the message.



      Is there a way in the outlook API to be able to utilize these graphs/charts that are available in the Insert Chart window? Or is the API relatively limited in features available compared to the application when it comes to message styling/formatting?



      Here's the bare-bones code for how the message is created/sent, I was wanting to add these visuals to the message body.



      import win32com.client as win32

      outlook = win32.Dispatch('outlook.application')
      mail = outlook.CreateItem(0)

      mail.To = 'some@name'
      mail.Subject = 'Subject'
      mail.Body = 'Body'
      # or
      mail.HTMLBody = '<h2>HTML body</h2>'

      mail.Send()




      Update



      I'm currently looking into it more, but I found this for vba that's supposed to do what I'm looking for. I've been messing around with chart objects a bit in excel as well. Here's the code I need to understand



      Sub CopyAndPasteToMailBody()
      Set mailApp = CreateObject("Outlook.Application")
      Set mail = mailApp.CreateItem(olMailItem)
      mail.Display
      Set wEditor = mailApp.ActiveInspector.wordEditor
      ActiveChart.ChartArea.Copy
      wEditor.Application.Selection.Paste
      End Sub




      Here's some bare bones code I have that will create a chart object:



      from win32com import client

      excel = client.Dispatch("excel.application")
      excel.Visible = True
      wb = excel.Workbooks.Add()
      ws = wb.Worksheets.Add()
      chs = ws.ChartObjects()
      co = chs.Add(0,0,500,500)




      Update



      Below I have answered how to spawn a chart in Excel and have it sent through Outlook. It is not the 'Outlook API', as it's Office as a whole that must be utilized. If you were confused like I was by a lack of documentation for using win32com to interact with Office applications, that's because it's all here in the VBA Reference and just needs to be converted into python accordingly.







      python vba outlook-restapi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 20:02

























      asked Nov 9 at 22:57









      saniboy

      939




      939
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          After doing more testing, I've figured it out. Here's a piece of what I made with sample data I had laying around. Note the line ch.ChartType = 5 # Pie chart as this answers the question. Yes, the graphs and charts can be accessed via the API, and ChartTypes can be found here. This answers the specific question, as well as the greater question of how to take data that exists outside of Office and load it into a chart to be sent through Outlook via the API.



          from win32com import client
          from random import randint

          labels =
          ch_data =

          for x,y in _data:
          labels.append(x)
          ch_data.append(y)

          _range = range(1,len(labels)+1)
          l_plot = map(lambda x: 'A'+str(x),_range)
          d_plot = map(lambda x: 'B'+str(x),_range)
          start_end = '{}:{}'.format(l_plot[0],d_plot[-1])

          excel = client.Dispatch('excel.application')
          excel.Visible = True # optional
          wb = excel.Workbooks.Add()
          ws = wb.Worksheets.Add()

          cnt = 0
          for i in l_plot:
          ws.Range(i).Value = labels[cnt]
          cnt += 1

          cnt = 0
          for i in d_plot:
          ws.Range(i).Value = ch_data[cnt]
          cnt += 1

          chs = ws.ChartObjects()
          co = chs.Add(100,0,250,250)
          ch = co.Chart
          ch.HasTitle = True
          ch.ChartTitle.Text = 'Chart Title'
          ch.ChartType = 5 # Pie chart

          series = ch.SeriesCollection()
          series.Extend(ws.Range(start_end))
          co.Copy()

          outlook = client.Dispatch('outlook.application')
          mail = outlook.CreateItem(0)
          mail.Display() # required to paste chart
          mail.To = 'alias@email.com'
          mail.Subject = 'Test Mail Item with Chart'
          mail.HTMLBody = '<h3>Hello sir. This is a test</h3><br><br>'

          inspector = mail.GetInspector
          editor = inspector.WordEditor
          editor.Select()
          editor.Application.Selection.Start = editor.Application.Selection.End
          editor.Application.Selection.Paste()

          mail.HTMLBody += '<p>I'm glad this is finally figured out</p>'
          mail.Send()





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234320%2fsend-graphs-in-outlook-with-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            After doing more testing, I've figured it out. Here's a piece of what I made with sample data I had laying around. Note the line ch.ChartType = 5 # Pie chart as this answers the question. Yes, the graphs and charts can be accessed via the API, and ChartTypes can be found here. This answers the specific question, as well as the greater question of how to take data that exists outside of Office and load it into a chart to be sent through Outlook via the API.



            from win32com import client
            from random import randint

            labels =
            ch_data =

            for x,y in _data:
            labels.append(x)
            ch_data.append(y)

            _range = range(1,len(labels)+1)
            l_plot = map(lambda x: 'A'+str(x),_range)
            d_plot = map(lambda x: 'B'+str(x),_range)
            start_end = '{}:{}'.format(l_plot[0],d_plot[-1])

            excel = client.Dispatch('excel.application')
            excel.Visible = True # optional
            wb = excel.Workbooks.Add()
            ws = wb.Worksheets.Add()

            cnt = 0
            for i in l_plot:
            ws.Range(i).Value = labels[cnt]
            cnt += 1

            cnt = 0
            for i in d_plot:
            ws.Range(i).Value = ch_data[cnt]
            cnt += 1

            chs = ws.ChartObjects()
            co = chs.Add(100,0,250,250)
            ch = co.Chart
            ch.HasTitle = True
            ch.ChartTitle.Text = 'Chart Title'
            ch.ChartType = 5 # Pie chart

            series = ch.SeriesCollection()
            series.Extend(ws.Range(start_end))
            co.Copy()

            outlook = client.Dispatch('outlook.application')
            mail = outlook.CreateItem(0)
            mail.Display() # required to paste chart
            mail.To = 'alias@email.com'
            mail.Subject = 'Test Mail Item with Chart'
            mail.HTMLBody = '<h3>Hello sir. This is a test</h3><br><br>'

            inspector = mail.GetInspector
            editor = inspector.WordEditor
            editor.Select()
            editor.Application.Selection.Start = editor.Application.Selection.End
            editor.Application.Selection.Paste()

            mail.HTMLBody += '<p>I'm glad this is finally figured out</p>'
            mail.Send()





            share|improve this answer



























              up vote
              0
              down vote



              accepted










              After doing more testing, I've figured it out. Here's a piece of what I made with sample data I had laying around. Note the line ch.ChartType = 5 # Pie chart as this answers the question. Yes, the graphs and charts can be accessed via the API, and ChartTypes can be found here. This answers the specific question, as well as the greater question of how to take data that exists outside of Office and load it into a chart to be sent through Outlook via the API.



              from win32com import client
              from random import randint

              labels =
              ch_data =

              for x,y in _data:
              labels.append(x)
              ch_data.append(y)

              _range = range(1,len(labels)+1)
              l_plot = map(lambda x: 'A'+str(x),_range)
              d_plot = map(lambda x: 'B'+str(x),_range)
              start_end = '{}:{}'.format(l_plot[0],d_plot[-1])

              excel = client.Dispatch('excel.application')
              excel.Visible = True # optional
              wb = excel.Workbooks.Add()
              ws = wb.Worksheets.Add()

              cnt = 0
              for i in l_plot:
              ws.Range(i).Value = labels[cnt]
              cnt += 1

              cnt = 0
              for i in d_plot:
              ws.Range(i).Value = ch_data[cnt]
              cnt += 1

              chs = ws.ChartObjects()
              co = chs.Add(100,0,250,250)
              ch = co.Chart
              ch.HasTitle = True
              ch.ChartTitle.Text = 'Chart Title'
              ch.ChartType = 5 # Pie chart

              series = ch.SeriesCollection()
              series.Extend(ws.Range(start_end))
              co.Copy()

              outlook = client.Dispatch('outlook.application')
              mail = outlook.CreateItem(0)
              mail.Display() # required to paste chart
              mail.To = 'alias@email.com'
              mail.Subject = 'Test Mail Item with Chart'
              mail.HTMLBody = '<h3>Hello sir. This is a test</h3><br><br>'

              inspector = mail.GetInspector
              editor = inspector.WordEditor
              editor.Select()
              editor.Application.Selection.Start = editor.Application.Selection.End
              editor.Application.Selection.Paste()

              mail.HTMLBody += '<p>I'm glad this is finally figured out</p>'
              mail.Send()





              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                After doing more testing, I've figured it out. Here's a piece of what I made with sample data I had laying around. Note the line ch.ChartType = 5 # Pie chart as this answers the question. Yes, the graphs and charts can be accessed via the API, and ChartTypes can be found here. This answers the specific question, as well as the greater question of how to take data that exists outside of Office and load it into a chart to be sent through Outlook via the API.



                from win32com import client
                from random import randint

                labels =
                ch_data =

                for x,y in _data:
                labels.append(x)
                ch_data.append(y)

                _range = range(1,len(labels)+1)
                l_plot = map(lambda x: 'A'+str(x),_range)
                d_plot = map(lambda x: 'B'+str(x),_range)
                start_end = '{}:{}'.format(l_plot[0],d_plot[-1])

                excel = client.Dispatch('excel.application')
                excel.Visible = True # optional
                wb = excel.Workbooks.Add()
                ws = wb.Worksheets.Add()

                cnt = 0
                for i in l_plot:
                ws.Range(i).Value = labels[cnt]
                cnt += 1

                cnt = 0
                for i in d_plot:
                ws.Range(i).Value = ch_data[cnt]
                cnt += 1

                chs = ws.ChartObjects()
                co = chs.Add(100,0,250,250)
                ch = co.Chart
                ch.HasTitle = True
                ch.ChartTitle.Text = 'Chart Title'
                ch.ChartType = 5 # Pie chart

                series = ch.SeriesCollection()
                series.Extend(ws.Range(start_end))
                co.Copy()

                outlook = client.Dispatch('outlook.application')
                mail = outlook.CreateItem(0)
                mail.Display() # required to paste chart
                mail.To = 'alias@email.com'
                mail.Subject = 'Test Mail Item with Chart'
                mail.HTMLBody = '<h3>Hello sir. This is a test</h3><br><br>'

                inspector = mail.GetInspector
                editor = inspector.WordEditor
                editor.Select()
                editor.Application.Selection.Start = editor.Application.Selection.End
                editor.Application.Selection.Paste()

                mail.HTMLBody += '<p>I'm glad this is finally figured out</p>'
                mail.Send()





                share|improve this answer














                After doing more testing, I've figured it out. Here's a piece of what I made with sample data I had laying around. Note the line ch.ChartType = 5 # Pie chart as this answers the question. Yes, the graphs and charts can be accessed via the API, and ChartTypes can be found here. This answers the specific question, as well as the greater question of how to take data that exists outside of Office and load it into a chart to be sent through Outlook via the API.



                from win32com import client
                from random import randint

                labels =
                ch_data =

                for x,y in _data:
                labels.append(x)
                ch_data.append(y)

                _range = range(1,len(labels)+1)
                l_plot = map(lambda x: 'A'+str(x),_range)
                d_plot = map(lambda x: 'B'+str(x),_range)
                start_end = '{}:{}'.format(l_plot[0],d_plot[-1])

                excel = client.Dispatch('excel.application')
                excel.Visible = True # optional
                wb = excel.Workbooks.Add()
                ws = wb.Worksheets.Add()

                cnt = 0
                for i in l_plot:
                ws.Range(i).Value = labels[cnt]
                cnt += 1

                cnt = 0
                for i in d_plot:
                ws.Range(i).Value = ch_data[cnt]
                cnt += 1

                chs = ws.ChartObjects()
                co = chs.Add(100,0,250,250)
                ch = co.Chart
                ch.HasTitle = True
                ch.ChartTitle.Text = 'Chart Title'
                ch.ChartType = 5 # Pie chart

                series = ch.SeriesCollection()
                series.Extend(ws.Range(start_end))
                co.Copy()

                outlook = client.Dispatch('outlook.application')
                mail = outlook.CreateItem(0)
                mail.Display() # required to paste chart
                mail.To = 'alias@email.com'
                mail.Subject = 'Test Mail Item with Chart'
                mail.HTMLBody = '<h3>Hello sir. This is a test</h3><br><br>'

                inspector = mail.GetInspector
                editor = inspector.WordEditor
                editor.Select()
                editor.Application.Selection.Start = editor.Application.Selection.End
                editor.Application.Selection.Paste()

                mail.HTMLBody += '<p>I'm glad this is finally figured out</p>'
                mail.Send()






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 at 3:57

























                answered Nov 14 at 19:49









                saniboy

                939




                939






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234320%2fsend-graphs-in-outlook-with-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini