2014-03-25 85 views
1

我正在尋找一種方式插入圖像到ironpython中的文字文件。 我想通過調用clr模塊並添加參考microsoft.office.interop.word來替換文本,但我無法將圖像寫入Word文檔。 我正在同:插入圖像在霓虹燈與word

import sys 
import clr 
import System 
from System import DateTime 
clr.AddReference("Microsoft.Office.Interop.Word") 
import Microsoft.Office.Interop.Word as Word 

def doc_replace_text(source_filename, tokens, values, destination_filename): 

    missing = System.Type.Missing 
    replaceAll = Word.WdReplace.wdReplaceAll 

    word_application = Word.ApplicationClass() 
    word_application.visible = False 

    document = word_application.Documents.Open(source_filename) 

    for i in range(len(tokens)): 
    for r in document.StoryRanges: 
     print "i = %d, tokens[i] = %s, values[i] = %s" % (i, tokens[i], values[i]) 
     r.Find.Text = tokens[i] 
     r.Find.Replacement.Text = values[i] 
     r.Find.Wrap = Word.WdFindWrap.wdFindContinue 
     r.Find.Execute(missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, replaceAll, missing, missing, missing, missing) 

    document.SaveAs(destination_filename) 
    document.Close() 
    document = None 

    word_application.Quit() 
    word_application = None 

我想感謝所有幫助

回答

1

我理解了它莫名其妙...

clr.AddReference("Microsoft.Office.Interop.Word") 
    from Microsoft.Office.Interop.Word import ApplicationClass 
    word=ApplicationClass() 
    word.Visible=False 
    docPublic=word.Documents.Add() 
    mod=docPublic.InlineShapes.AddPicture(path+name+".jpg", False, True, docPublic.Paragraphs[1].Range) 
    mod.Height=200 
    mod.Width=300 
    docPublic.SaveAs(tpath+name+"_Simulation_Report_Public.docx") 
    docPublic.Close() 
    docPublic=None 
    word.Quit() 
    word=None 

但是現在我需要將圖片放置到其他位置。我認爲這可以通過使用形狀的東西來完成...

任何幫助將是巨大的

編輯:看來我沒有從任何msdn.microsoft想法我在做什麼... .com AddPicture方法取值爲: FileName,LinkToFile,SaveWithDocument,Left,Top,Width,Height,Anchor