使用Python发送包含Outlook Equation的电子邮件

时间:2017-06-18 13:18:38

标签: python html email outlook

我想每天自动发送一封包含Python的电子邮件。在电子邮件中,我想要一个等式来解释我的电子邮件中的数字是如何计算的。

我的所有收件人都使用Outlook作为其电子邮件桌面客户端。有没有办法将公式括在一个等式中,而不是当前的简单等式$ f(A)-f(B)$。

任何提示都将受到高度赞赏!

2 个答案:

答案 0 :(得分:1)

以下是一些可能性:

MS Outlook可以以HTML格式发送电子邮件。 HTML可以包含嵌入的图像,数学公式可以在LaTex中编码,然后转换为可嵌入HTML的图像。因此,一种方法是使用诸如the Daum Equation Editor之类的产品将公式编码到LaTex中并将结果保存为例如Jpeg文件。根据应用程序的不同,可以对公式进行参数化,以便可以替换LaTex的元素,而无需使用Daum产品,然后使用Python代码转换为图像。

MS Office产品具有可使用win32com库使用Python访问的COM接口。探索这个的一个好方法是使用PythonWin REPL。

由于Outlook可以通过其GUI操作,因此可以使用键盘和鼠标手势使用Python通过PyAutoGUI等库来编码等式。

编辑:

我只是寻找其他可能性并被提醒:https://stackoverflow.com/a/36249956/131187

答案 1 :(得分:1)

我从网上收集了很多零碎的东西

这是一个有效的测试程序

刚刚打开"新电子邮件"然后运行程序(或单步)

该计划的后半部分只是为了展示如何操纵电子邮件正文中的文字

它只是VBA,但它可能会给你一个起点

我将看一下下面的python接口

问候

JS:)

' NOTE:    make sure that you have a reference to "microsoft word object library"

'          go to "tools" ... "references" ... find microsoft word object library in dropdown and click on checkmark box 



' this is just some test text
Const a1 = "1111111111 1234567890 1111111133 1111111144 1111111155"
Const a2 = "2222222211 2222222222 2222222233 2222222244 2222222255"
Const a3 = "3333333311 3333333322 3333333333 3333333344 3333333355"
Const a4 = "3333333366 3333333377 3333333388 3333333399 3333333300"
Const a5 = "4444444411 4444444422 4444444433 4444444444 4444444455"





Sub test1234()

    Dim wordDoc As Document
    Set wordDoc = Application.ActiveInspector.WordEditor

    wordDoc.Range.Delete                 ' clean page
    wordDoc.Paragraphs.Space1            ' single space paragraph
    wordDoc.Paragraphs.SpaceBefore = 0   ' no space before paragraph
    wordDoc.Paragraphs.SpaceAfter = 0    ' no space after paragraph
    wordDoc.Range.InsertAfter (a1)
    wordDoc.Range.InsertParagraphAfter   ' paragraph mark
    wordDoc.Range.InsertAfter (a2)
    wordDoc.Range.InsertParagraphAfter
    wordDoc.Range.InsertAfter (a3)
    wordDoc.Range.InsertAfter Chr(11)    ' linebreak
    wordDoc.Range.InsertAfter (a4)
    wordDoc.Range.InsertParagraphAfter
    wordDoc.Range.InsertAfter (a5)
    wordDoc.Range.InsertParagraphAfter

    Stop

    Dim objRange As Range
    Dim rng As Range
    Dim objEq As oMath
    Dim formula As String

    ' check this webpage re. problems with oMath
    ' https://stackoverflow.com/questions/20068212/excel-word-equations-using-omath-buildup-method#20072757

    'formula = "Celsius = (5/9)(Fahrenheit - 32)"
    formula = "Celsius = " & ChrW(&H221A) & "(x+y) + sin(5/9 × (Fahrenheit – 23 (" & ChrW(&H3B4) & ")^2))"

    wordDoc.Range.InsertParagraphAfter
    wordDoc.Range.InsertAfter (formula)
    wordDoc.Range.InsertParagraphAfter

    Stop        

    Offset = Len(formula)

    Set rng = wordDoc.Range(wordDoc.Sentences(5).Characters(1).Start, wordDoc.Sentences(5).Characters(Offset).End)
    rng.Select    ' this line is not necessary, but it shows size and position of the range when single-stepping through the program

    Set objRange = wordDoc.OMaths.Add(rng)
'    objRange.OMaths(1).BuildUp
    wordDoc.OMaths(1).BuildUp

    Application.ActiveInspector.currentItem.Display       ' not sure what this does, exactly ... may not be necessary
    Application.ActiveInspector.currentItem.Save          ' save email draft
'   Application.ActiveInspector.currentItem.Close olSave

    Stop

' the equation should be in the email body at this point
' ------------------------------------------------------



' ------ following is just code that "plays around" with the text

' ------ it may be of interest to someone


    response = InputBox("Type some text")
    With wordDoc.Paragraphs(wordDoc.Paragraphs.Count).Range
        .InsertAfter "1." & vbTab & response
        .InsertParagraphAfter
    End With

    Stop

    wordDoc.Undo
    wordDoc.Undo

    Stop

    wordDoc.Range.Delete
    wordDoc.Undo
    wordDoc.Paragraphs(1).Range.Delete unit:=wdWord, Count:=3    ' delete 1st three words
    wordDoc.Undo
    wordDoc.Sentences(1).Words(2).Delete unit:=wdWord, Count:=2  ' delete words 2 and 3
    wordDoc.Undo
    wordDoc.Sentences(1).Words(2).Characters(4).Delete unit:=wdCharacter, Count:=2  ' delete characters in second word
    wordDoc.Undo

'    Debug.Print wordDoc.Sentences(1).Characters.Count

'    Debug.Print wordDoc.Words.Count       ' EOL counts as a word
'    For i = 1 To wordDoc.Words.Count
'        Debug.Print wordDoc.Words(i)
'    Next
    Stop

'    Dim word As Range
'    Debug.Print wordDoc.Sentences(2).Words.Count       ' EOL counts as a word
'    For Each word In wordDoc.Sentences(2).Words
'        Debug.Print word
'    Next
    Stop

'   these two are the same
'   Application.ActiveInspector.currentItem.GetInspector.WordEditor.Characters(2).InsertBefore "___xxxxxx___"
'   Application.ActiveInspector.WordEditor.Characters(2).InsertBefore "___xxxxxx___"

    wordDoc.Characters(1).Select               ' 1st character from top of page
    wordDoc.Characters(10).Select              ' 10th character from top of page
    wordDoc.Range(0, 5).Select                 ' 1nd to 5th character
    wordDoc.Range(1, 5).Select                 ' 2nd to 5th character

    Debug.Print wordDoc.Characters(1)          ' 1
    Debug.Print wordDoc.Characters(1).Start    ' 0
    Debug.Print wordDoc.Characters(1).End      ' 1
    Debug.Print wordDoc.Characters(1)          ' 1


'    wordDoc.Sentences(3).Select                     ' all of 3rd sentence (paragraph)
'    wordDoc.Sentences(4).Characters(5).Select       ' 5th character of 4th sentence

'    Debug.Print wordDoc.Sentences(4).Characters(3)  ' !!!! position of 2nd character on PAGE !!!!

'    Debug.Print wordDoc.Sentences(2).Characters(3).Start   ' position of 3rd character in 2nd sentence
'    Debug.Print wordDoc.Sentences(2).Characters(3).End     ' position AFTER 3rd character in 2nd sentence

'    wordDoc.Range(wordDoc.Sentences(2).Characters(3).Start, wordDoc.Sentences(3).Characters(5).End).Select
'    wordDoc.Range(wordDoc.Sentences(2).Characters(3).End, wordDoc.Sentences(3).Characters(5).End).Select

'    wordDoc.Sentences(2).InsertBefore "__ before sentence #2 __"
'    wordDoc.Sentences(3).InsertAfter "__ after sentence #3 __"


End Sub