我想每天自動發送一封包含Python的電子郵件。在電子郵件中,我想要解釋如何計算電子郵件中的電話號碼。用Python發送電子郵件包括Outlook公式
我的所有收件人都使用Outlook作爲他們的電子郵件桌面客戶端。有沒有什麼辦法可以將公式包含在方程中,而不是使用簡單方程$ f(A)-f(B)$的純文本。
任何提示將不勝感激!
我想每天自動發送一封包含Python的電子郵件。在電子郵件中,我想要解釋如何計算電子郵件中的電話號碼。用Python發送電子郵件包括Outlook公式
我的所有收件人都使用Outlook作爲他們的電子郵件桌面客戶端。有沒有什麼辦法可以將公式包含在方程中,而不是使用簡單方程$ f(A)-f(B)$的純文本。
任何提示將不勝感激!
以下是一些可能性:
MS Outlook可以以HTML格式發送電子郵件。 HTML可以包含嵌入圖像,數學公式可以在LaTex中編碼,然後轉換爲可嵌入HTML的圖像。因此,一種方法是使用諸如the Daum Equation Editor之類的產品將公式編碼到LaTex中並將結果保存爲例如Jpeg文件。根據應用的不同,可能需要對公式進行參數化,以便可以替換LaTex的元素,而無需使用Daum產品,然後使用Python代碼轉換爲圖像。
MS Office產品具有可使用Python使用win32com
庫訪問的COM接口。探索這個好方法是使用PythonWin REPL。
由於Outlook可以通過它的GUI進行操作,因此可以使用按鍵和鼠標手勢通過庫(如PyAutoGUI)使用Python對公式進行編碼。
編輯:
我只是看着其他的可能性,並提醒這一點:https://stackoverflow.com/a/36249956/131187。
編輯:添加另一種可能性。 –
我組裝了很多點點滴滴的從網絡
這裏是工作的測試程序
只需打開「新郵件」,然後運行程序(或單步)
的該計劃的第二部分是隻是爲了顯示如何在電子郵件正文中處理文本
它只是VBA,但它可能給你一個起點
我會看看接下來
問候
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
是Python接口。例如,使用http://s1.daumcdn.net/editor/fp/service_nc/pencil/Pencil_chromestore.htm將'$ f(A)-f(B)$'轉換爲jpeg圖像。將您的消息與該圖像嵌入HTML併發送。我懷疑我們需要更多細節。 –
@BillBell感謝您的回覆和真正的輝煌解決方案。是否可以將方程直接編碼爲電子郵件,就像Outlook「插入方程」本身所做的一樣? –
很可能,我沒有嘗試過。 (1)MS Office產品具有可以使用Python訪問和操作的COM接口。 (2)由於Outlook可以通過它的GUI進行操作,因此可以使用Python通過https://pyautogui.readthedocs.io/en/latest/等庫進行擊鍵編碼。 –