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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_tmx-0.2.0.2.tar.gz
.
File metadata
- Download URL: python_tmx-0.2.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.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55c9287097c02e30af4c66e63268f93a0e09d539874552f1cf811ec1de99cb34 |
|
MD5 | 3c9180730e5ab7e631b7ce09901c5439 |
|
BLAKE2b-256 | b751f4b436455d031ea0dd888bd111d3428d0040a8c2c8f518ffafa1d428fb5c |
File details
Details for the file python_tmx-0.2.0.2-py3-none-any.whl
.
File metadata
- Download URL: python_tmx-0.2.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.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff1ea561bf5c8e24fb74eea60476431937cff39056276e3f4bc4765f0c5c1021 |
|
MD5 | 3d2969663bcf40afddc23261a0e2f11f |
|
BLAKE2b-256 | 355bf1d6a573850b5841d7b97c5991ac475f9343b5c58308da6f40e32f4059d2 |