This repository provides a Python script for translating .docx
files using OpenAI’s GPT(-4) API.
.docx
files and writes translated output to .docx
files while preserving paragraph structure.openai
python-docx
tiktoken
To install the required libraries, run:
pip install openai python-docx tiktoken
## Setup
### Configuration File
Create a `config.json` file in the same directory as the script and add your OpenAI API key:
```json
{
"OPENAI_API_KEY": "your-api-key-here"
}
.docx
file you want to translate..docx
file with sample translations to guide the translation style.Open the script and modify the main()
function call:
main('input.docx', 'output.docx', sample_translation_file='sample_translation.docx')
input.docx
with the path to your input file.output.docx
with the desired output file path.Run the script; e.g. in bash:
python openai_translator.py
The translated document will be saved as the specified output file.