簡體   English   中英

將不同內容類型的MHT文件提取到多個MHT文件中

[英]Extracting different content-type of MHT file into multiple mht file

我正在編寫一個mht腳本來解析mht文件並從父級提取部分消息並將其寫入單獨的mht文件

我編寫了以下函數,該函數在file_location打開一個mht文件並搜索特定的content_id並將其寫入新的mht文件

def extract_content(self, file_location, content_id,extension):
    first_part = file_location.split(extension)[0]
    #checking if file exists
    new_file = first_part + "-" + content_id.split('.')[0] + extension

    while os.path.exists(new_file):
        os.remove(new_file)

    with open(file_location, 'rb') as mime_file, open(new_file, 'w') as output:
        ***#Extracting the message from the mht file***
        message = message_from_file(mime_file)
        t = mimetypes.guess_type(file_location)[0]

        #Walking through the message
        for i, part in enumerate(message.walk()):

            #Check the content_id if the one we are looking for
            if part['Content-ID'] == '<' + content_id + '>':
                ***witing the contents***
                output.write(part.as_string(unixfrom=False))

顯然,在application / pdf和application / octet-stream的情況下,我無法在IE中打開輸出部分。

我如何將這些Content-Type(如application / pdf和application / octet-stream)寫入mht文件,以便能夠在IE中查看圖像或pdf?

謝謝

嘗試這個:

...
if m['Content-type'].startswith('text/'):
                    m["Content-Transfer-Encoding"] = "quoted-printable"

                else:
                    m["Content-Transfer-Encoding"] = "base64"

                m.set_payload(part.get_payload())                        
                ****Writing to output****
                info = part.as_string(unixfrom=False)
                info = info.replace('application/octet-stream', 'text/plain')
                output.write(info)
...

告訴我是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM