Python script to parse MQL4/5 log files to Excel workbooks for filtering and analysis.

 

python requirements: easygui, openpyxl

import os
import re
import sys
from pathlib import Path

import easygui as ez
from openpyxl import Workbook


def parse_log(file: str):
    if not Path(file).match('*.log'):
        raise Exception('Error: Not a valid log file')
    if 'MQL4' in file:
        p = re.compile(
            r"(\d)\s(.*?)\s(?:Expert|Script)?\s?(.*?)\s(.*?),(.*?)(?:\s?inputs)?:\s(.*)")
        encoding = 'ascii'
    else:
        p = re.compile(
            r"\w{2}\s(\d)\s(.*?)\s(.*?)\s\((.*?),(.*?)\)\s(.*)")
        encoding = 'utf-16'
    rows = []
    with open(file, encoding=encoding) as f:
        for line in f:
            m = p.match(line)
            if m:
                rows.append(m.groups())
    return rows


def main():
    print('Program loading...')
    check = ez.boolbox(
        'This program will convert a selected MQL log file to an Excel Workbook.\n'
        'Would you like to continue?',
        choices=['[Y]es', '[N]o']
    )
    if not check:
        sys.exit()
    try:
        home = Path.home()
        terminals = home / 'AppData/Roaming/MetaQuotes/Terminal'
        path = terminals if terminals.exists() else home
        file = ez.fileopenbox(
            'Select MQL log file'.upper(),
            default=f'{path}\\*.log',
            filetypes=['*.log']
        )
        if file is None:
            sys.exit()
        rows = parse_log(file)
        wb = Workbook()
        ws1 = wb.active
        ws1.title = 'log'
        ws1.append('type time program symbol timeframe message'.upper().split())
        for row in rows:
            ws1.append(row)
        xl_file = ez.filesavebox(
            'Where to save excel file?'.upper(),
            default=f'{home}\\Desktop\\mql_log.xlsx',
            filetypes=['*.xlsx']
        )
        wb.save(xl_file)
        os.startfile(xl_file)
    except Exception as e:
        sys.exit(ez.msgbox(str(e)))


if __name__ == '__main__':
    main()
Reason: