Skip to main content

Python library for manipulating, creating and editing tmx files

Project description

Overview

PythonTmx is a Python library that aims to make dealing with tmx files in Python easier.

Every tmx element from the TMX 1.4b standard is represented (including the deprecated element for compatibility).

To create a Tmx Element, you can either create it from scratch and set its attributes using keyword arguments, or you can also give it a lxml element to parse and use a base. If you give both an lxml element and keyword arguments the values parsed from the element will take precedence and the keywords argument values will be used as a fallback.

Installing

Simply download from Pypi and install using pip:

pip install --upgrade PythonTmx

Basic usage

You can create a Tmx object by reading a tmx file. From there, you can just iterate over each tu, and further each tuv, do any processing needed and then just export it back to another file.

from datetime import UTC, datetime

from PythonTmx import from_tmx
from PythonTmx.inline import Ph

# Create Tmx object from a tmx file
tmx_file = from_tmx("tmx_file.tmx")
# Loop over all Tu
for tu in tmx_file:
    # Set the Tu's source language to English
    tu.srclang = "EN"
    # Loop over all Tuv and check the language
    for tuv in tu.tuvs:
        match tuv.xmllang:
            case "EN":
                # If Tuv is in English, add _English at the end
                tuv.segment.append("_English")
            case "FR":
                # If it's French, add a Ph that says "_French"
                # at the start
                tuv.segment.insert(0, Ph(content="_French"))
            case _:
                # Remove the Tuv otherwise
                tu.tuvs.remove(tuv)
        # Add a prop to the Tuv and set the changedate to today
        tuv.add_prop(type="x-confidential", text="True")
        tuv.changedate = datetime.now(UTC)
    # Remove all notes that don't start with "Hello"
    # and also add a new one and set the changedate to today
    for note in tu.notes:
        if not note.text.startswith("Hello"):
            tu.remove_note(note)
    tu.add_note(text="adding a note here, huge success")
    tu.changedate = datetime.now(UTC)
# Export Tmx to a tmx file and to a csv file
tmx_file.to_tmx("bilingual_tmx_file.tmx")
tmx_file.to_csv("bilingual.csv")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_tmx-0.2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

python_tmx-0.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file python_tmx-0.2.tar.gz.

File metadata

  • Download URL: python_tmx-0.2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for python_tmx-0.2.tar.gz
Algorithm Hash digest
SHA256 6b805242d0c247e3a0e27e91dce20b85f05844951d7c4178993a3f73bc3eaa90
MD5 43fa23592cca386726880917ef7fc068
BLAKE2b-256 da85eb1e92327e2c4378c6060079db9606fdb17ce9531c9e8b7fd8117088a6af

See more details on using hashes here.

File details

Details for the file python_tmx-0.2-py3-none-any.whl.

File metadata

  • Download URL: python_tmx-0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for python_tmx-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d6c3bdda56274a69871b9c7d11a25b50c8fcace70453e1248da4bff453ccb237
MD5 24d9a98f0b87e0788730cb8beea56a3b
BLAKE2b-256 bae92d5e5139829a4ff4f53d5da5534ac1dbe3848cb8dec0fd9cc94bbe311584

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page