Tomy
Tomy

Reputation: 247

How can i open pst file (file of outlook mail ) with python?

I have pst file and i want open that with python, how can i do?

i try this library but dont work.

my code is:

import mailparser

mail = mailparser.parse_from_bytes(byte_mail)
mail = mailparser.parse_from_file('c/document/1.pst')
print(mail.attachments)  #list of all attachments

but get this errore : mailparser.exceptions.MailParserOSError: To use this function you must install 'msgconvert' tool

----------All error text:-----------------
To use this function you must install 'msgconvert' tool
Traceback (most recent call last):
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mailparser\utils.py", line 229, in msgconvert
    stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Traceback (most recent call last):
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mailparser\utils.py", line 229, in msgconvert
    stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Drive D/my projects/azar/untitled java 13 12  2019/Pst mail.py", line 3, in <module>
    mail=mailparser.parse_from_file_msg('C:/Users/aa/Documents/1.pst')
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mailparser\mailparser.py", line 89, in parse_from_file_msg
    return MailParser.from_file_msg(fp)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mailparser\mailparser.py", line 203, in from_file_msg
    f, _ = msgconvert(fp)
  File "C:\Users\aa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mailparser\utils.py", line 234, in msgconvert
    raise MailParserOSError(message)
mailparser.exceptions.MailParserOSError: To use this function you must install 'msgconvert' tool

-----------------------------------

Upvotes: 0

Views: 7793

Answers (3)

Tomy
Tomy

Reputation: 247

my solution: I convert .pst file to .msg file and read that with python (with app: BitRecover PST Converter Wizard 10.6).

import extract_msg 
msg = extract_msg.Message("C:/Users/aa/Documents/delete/Untitled.msg") 
print(msg.attachments)`

Upvotes: 0

prasad
prasad

Reputation: 2026

This one seems to work, https://pypi.org/project/libratom/

from libratom.lib.pff import PffArchive
from email import generator
from pathlib import Path

archive = PffArchive("bill_rapp_000_1_1.pst")
eml_out = Path(Path.cwd() / "emls")

if not eml_out.exists():
  eml_out.mkdir()

print("Writing messages to .eml")
for folder in archive.folders():
    if folder.get_number_of_sub_messages() != 0:
        for message in folder.sub_messages:
            name = message.subject.replace(" ", "_")
            name = name.replace("/","-")
            filename = eml_out / f"{message.identifier}_{name}.eml"
            filename.write_text(archive.format_message(message))
print("Done!")

Upvotes: 2

Anurag
Anurag

Reputation: 901

You need to run the python script in linux environment only.

You need to install Linux package using following command:

$ apt-get install libemail-outlook-message-perl

Please go through the description on below link: https://pypi.org/project/mail-parser/

Upvotes: 0

Related Questions