rtfparse


Namertfparse JSON
Version 0.9.4 PyPI version JSON
download
home_pageNone
SummaryTool to parse Microsoft Rich Text Format (RTF)
upload_time2024-11-10 02:41:35
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseCopyright (c) 2023 Sven Siegmud Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords parse rtf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # rtfparse

Parses Microsoft's Rich Text Format (RTF) documents. It creates an in-memory object which represents the tree structure of the RTF document. This object can in turn be rendered by using one of the renderers.
So far, rtfparse provides only one renderer (`HTML_Decapsulator`) which liberates the HTML code encapsulated in RTF. This will come handy, for examle, if you ever need to extract the HTML from a HTML-formatted email message saved by Microsoft Outlook.

MS Outlook also tends to use RTF compression, so the CLI of rtfparse can optionally decompress that, too.

You can of course write your own renderers of parsed RTF documents and consider contributing them to this project.


# Installation

Install rtfparse from your local repository with pip:

    pip install rtfparse

Installation creates an executable file `rtfparse` in your python scripts folder which should be in your `$PATH`.

# Usage From Command Line

Use the `rtfparse` executable from the command line. Read `rtfparse --help`.

rtfparse writes logs into `~/rtfparse/` into these files:

```
rtfparse.debug.log
rtfparse.info.log
rtfparse.errors.log
```

## Example: Decapsulate HTML from an uncompressed RTF file

    rtfparse --rtf-file "path/to/rtf_file.rtf" --decapsulate-html --output-file "path/to/extracted.html"

## Example: Decapsulate HTML from MS Outlook email file

For this, the CLI of rtfparse uses [extract_msg](https://github.com/TeamMsgExtractor/msg-extractor) and [compressed_rtf](https://github.com/delimitry/compressed_rtf).

    rtfparse --msg-file "path/to/email.msg" --decapsulate-html --output-file "path/to/extracted.html"

## Example: Only decompress the RTF from MS Outlook email file

    rtfparse --msg-file "path/to/email.msg" --output-file "path/to/extracted.rtf"

## Example: Decapsulate HTML from MS Outlook email file and save (and later embed) the attachments

When extracting the RTF from the `.msg` file, you can save the attachments (which includes images embedded in the email text) in a directory:

    rtfparse --msg-file "path/to/email.msg" --output-file "path/to/extracted.rtf" --attachments-dir "path/to/dir"

In `rtfparse` version 1.x you will be able to embed these images in the decapsulated HTML. This functionality will be provided by the package [embedimg](https://github.com/fleetingbytes/embedimg).

    rtfparse --msg-file "path/to/email.msg" --output-file "path/to/extracted.rtf" --attachments-dir "path/to/dir" --embed-img

In the current version the option `--embed-img` does nothing.

# Programatic usage in a Python module

## Decapsulate HTML from an uncompressed RTF file

```py
from pathlib import Path
from rtfparse.parser import Rtf_Parser
from rtfparse.renderers.html_decapsulator import HTML_Decapsulator

source_path = Path(r"path/to/your/rtf/document.rtf")
target_path = Path(r"path/to/your/html/decapsulated.html")
# Create parent directory of `target_path` if it does not already exist:
target_path.parent.mkdir(parents=True, exist_ok=True)

parser = Rtf_Parser(rtf_path=source_path)
parsed = parser.parse_file()

renderer = HTML_Decapsulator()

with open(target_path, mode="w", encoding="utf-8") as html_file:
    renderer.render(parsed, html_file)
```

## Decapsulate HTML from an MS Outlook msg file

```py
from pathlib import Path
from extract_msg import openMsg
from compressed_rtf import decompress
from io import BytesIO
from rtfparse.parser import Rtf_Parser
from rtfparse.renderers.html_decapsulator import HTML_Decapsulator


source_file = Path("path/to/your/source.msg")
target_file = Path(r"path/to/your/target.html")
# Create parent directory of `target_path` if it does not already exist:
target_file.parent.mkdir(parents=True, exist_ok=True)

# Get a decompressed RTF bytes buffer from the MS Outlook message
msg = openMsg(source_file)
decompressed_rtf = decompress(msg.compressedRtf)
rtf_buffer = BytesIO(decompressed_rtf)

# Parse the rtf buffer
parser = Rtf_Parser(rtf_file=rtf_buffer)
parsed = parser.parse_file()

# Decapsulate the HTML from the parsed RTF
decapsulator = HTML_Decapsulator()
with open(target_file, mode="w", encoding="utf-8") as html_file:
    decapsulator.render(parsed, html_file)
```

# RTF Specification Links

* [RTF Informative References](https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfcp/85c0b884-a960-4d1a-874e-53eeee527ca6)
* [RTF Specification 1.9.1](https://go.microsoft.com/fwlink/?LinkId=120924)
* [RTF Extensions, MS-OXRTFEX](https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfex/411d0d58-49f7-496c-b8c3-5859b045f6cf)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "rtfparse",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "parse, rtf",
    "author": null,
    "author_email": "Sven Siegmund <sven.siegmund@iav.de>",
    "download_url": "https://files.pythonhosted.org/packages/80/b2/14b50fe4f463c64446a575c08f800e9ea655ad17c941353132de591e22ae/rtfparse-0.9.4.tar.gz",
    "platform": null,
    "description": "# rtfparse\n\nParses Microsoft's Rich Text Format (RTF) documents. It creates an in-memory object which represents the tree structure of the RTF document. This object can in turn be rendered by using one of the renderers.\nSo far, rtfparse provides only one renderer (`HTML_Decapsulator`) which liberates the HTML code encapsulated in RTF. This will come handy, for examle, if you ever need to extract the HTML from a HTML-formatted email message saved by Microsoft Outlook.\n\nMS Outlook also tends to use RTF compression, so the CLI of rtfparse can optionally decompress that, too.\n\nYou can of course write your own renderers of parsed RTF documents and consider contributing them to this project.\n\n\n# Installation\n\nInstall rtfparse from your local repository with pip:\n\n    pip install rtfparse\n\nInstallation creates an executable file `rtfparse` in your python scripts folder which should be in your `$PATH`.\n\n# Usage From Command Line\n\nUse the `rtfparse` executable from the command line. Read `rtfparse --help`.\n\nrtfparse writes logs into `~/rtfparse/` into these files:\n\n```\nrtfparse.debug.log\nrtfparse.info.log\nrtfparse.errors.log\n```\n\n## Example: Decapsulate HTML from an uncompressed RTF file\n\n    rtfparse --rtf-file \"path/to/rtf_file.rtf\" --decapsulate-html --output-file \"path/to/extracted.html\"\n\n## Example: Decapsulate HTML from MS Outlook email file\n\nFor this, the CLI of rtfparse uses [extract_msg](https://github.com/TeamMsgExtractor/msg-extractor) and [compressed_rtf](https://github.com/delimitry/compressed_rtf).\n\n    rtfparse --msg-file \"path/to/email.msg\" --decapsulate-html --output-file \"path/to/extracted.html\"\n\n## Example: Only decompress the RTF from MS Outlook email file\n\n    rtfparse --msg-file \"path/to/email.msg\" --output-file \"path/to/extracted.rtf\"\n\n## Example: Decapsulate HTML from MS Outlook email file and save (and later embed) the attachments\n\nWhen extracting the RTF from the `.msg` file, you can save the attachments (which includes images embedded in the email text) in a directory:\n\n    rtfparse --msg-file \"path/to/email.msg\" --output-file \"path/to/extracted.rtf\" --attachments-dir \"path/to/dir\"\n\nIn `rtfparse` version 1.x you will be able to embed these images in the decapsulated HTML. This functionality will be provided by the package [embedimg](https://github.com/fleetingbytes/embedimg).\n\n    rtfparse --msg-file \"path/to/email.msg\" --output-file \"path/to/extracted.rtf\" --attachments-dir \"path/to/dir\" --embed-img\n\nIn the current version the option `--embed-img` does nothing.\n\n# Programatic usage in a Python module\n\n## Decapsulate HTML from an uncompressed RTF file\n\n```py\nfrom pathlib import Path\nfrom rtfparse.parser import Rtf_Parser\nfrom rtfparse.renderers.html_decapsulator import HTML_Decapsulator\n\nsource_path = Path(r\"path/to/your/rtf/document.rtf\")\ntarget_path = Path(r\"path/to/your/html/decapsulated.html\")\n# Create parent directory of `target_path` if it does not already exist:\ntarget_path.parent.mkdir(parents=True, exist_ok=True)\n\nparser = Rtf_Parser(rtf_path=source_path)\nparsed = parser.parse_file()\n\nrenderer = HTML_Decapsulator()\n\nwith open(target_path, mode=\"w\", encoding=\"utf-8\") as html_file:\n    renderer.render(parsed, html_file)\n```\n\n## Decapsulate HTML from an MS Outlook msg file\n\n```py\nfrom pathlib import Path\nfrom extract_msg import openMsg\nfrom compressed_rtf import decompress\nfrom io import BytesIO\nfrom rtfparse.parser import Rtf_Parser\nfrom rtfparse.renderers.html_decapsulator import HTML_Decapsulator\n\n\nsource_file = Path(\"path/to/your/source.msg\")\ntarget_file = Path(r\"path/to/your/target.html\")\n# Create parent directory of `target_path` if it does not already exist:\ntarget_file.parent.mkdir(parents=True, exist_ok=True)\n\n# Get a decompressed RTF bytes buffer from the MS Outlook message\nmsg = openMsg(source_file)\ndecompressed_rtf = decompress(msg.compressedRtf)\nrtf_buffer = BytesIO(decompressed_rtf)\n\n# Parse the rtf buffer\nparser = Rtf_Parser(rtf_file=rtf_buffer)\nparsed = parser.parse_file()\n\n# Decapsulate the HTML from the parsed RTF\ndecapsulator = HTML_Decapsulator()\nwith open(target_file, mode=\"w\", encoding=\"utf-8\") as html_file:\n    decapsulator.render(parsed, html_file)\n```\n\n# RTF Specification Links\n\n* [RTF Informative References](https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfcp/85c0b884-a960-4d1a-874e-53eeee527ca6)\n* [RTF Specification 1.9.1](https://go.microsoft.com/fwlink/?LinkId=120924)\n* [RTF Extensions, MS-OXRTFEX](https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfex/411d0d58-49f7-496c-b8c3-5859b045f6cf)\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2023 Sven Siegmud\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Tool to parse Microsoft Rich Text Format (RTF)",
    "version": "0.9.4",
    "project_urls": {
        "Documentation": "https://github.com/fleetingbytes/rtfparse#readme",
        "Issues": "https://github.com/fleetingbytes/rtfparse/issues",
        "Source": "https://github.com/fleetingbytes/rtfparse"
    },
    "split_keywords": [
        "parse",
        " rtf"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8a72373656a31ebdf68e1350fa3b898eb8bfabfcda6ffc7e692162dd33bcd5f1",
                "md5": "f9c933679637e60a75f50633d8cb861c",
                "sha256": "93e23ed5809f927e5655a863556075a184ec89e5f13108cdc2155de169332871"
            },
            "downloads": -1,
            "filename": "rtfparse-0.9.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f9c933679637e60a75f50633d8cb861c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 15976,
            "upload_time": "2024-11-10T02:41:37",
            "upload_time_iso_8601": "2024-11-10T02:41:37.801757Z",
            "url": "https://files.pythonhosted.org/packages/8a/72/373656a31ebdf68e1350fa3b898eb8bfabfcda6ffc7e692162dd33bcd5f1/rtfparse-0.9.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "80b214b50fe4f463c64446a575c08f800e9ea655ad17c941353132de591e22ae",
                "md5": "ee3eb32d642250b86c5e26718018c80a",
                "sha256": "9af08c28c05e9238db6c237942722297895ef05ee859a4fc7b6bd01b7ba134c1"
            },
            "downloads": -1,
            "filename": "rtfparse-0.9.4.tar.gz",
            "has_sig": false,
            "md5_digest": "ee3eb32d642250b86c5e26718018c80a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 14419,
            "upload_time": "2024-11-10T02:41:35",
            "upload_time_iso_8601": "2024-11-10T02:41:35.919093Z",
            "url": "https://files.pythonhosted.org/packages/80/b2/14b50fe4f463c64446a575c08f800e9ea655ad17c941353132de591e22ae/rtfparse-0.9.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-10 02:41:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fleetingbytes",
    "github_project": "rtfparse#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "rtfparse"
}
        
Elapsed time: 0.39339s