-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunrtf.py
More file actions
31 lines (26 loc) · 775 Bytes
/
unrtf.py
File metadata and controls
31 lines (26 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import os
import sys
from contextlib import contextmanager
import _unrtf
@contextmanager
def stdoutRedirected(to=os.devnull):
fd = sys.stdout.fileno()
def _redirect_stdout(to):
sys.stdout.close()
os.dup2(to.fileno(), fd)
sys.stdout = os.fdopen(fd, 'w')
with os.fdopen(os.dup(fd), 'w') as old_stdout:
with open(to, 'w') as file:
_redirect_stdout(to=file)
try:
yield
finally:
_redirect_stdout(to=old_stdout)
def unrtf(rtf_data, output_file, no_pict_mode=True):
assert rtf_data is not None
if rtf_data.strip() == '':
return ''
with stdoutRedirected(to=output_file):
_unrtf.unrtf(rtf_data, no_pict_mode)