標準出力取得その2 
Writer ドキュメントを標準出力表示に利用します。
出力を確認できない環境でファイルに保存するのが面倒な人や、出力を直ぐ確認しないと困るときに利用します。出力を保存したい時などにも。
start_output 関数を実行すると Writer ドキュメントが開きます。ドキュメントを閉じるか、close_output 関数を呼び出すと標準出力を元に戻します。
0
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| | import sys
import unohelper
from com.sun.star.util import XCloseListener
class WriterAsOutput(unohelper.Base, XCloseListener):
""" Toy of alternative output window.
If you change default font of standard paragraph style to
mono type and change mode to print layout, you can get
much better look.
"""
Out = None
def __init__(self):
self.doc = None
self._stdout = None
self._stderr = None
def disposing(self, ev): pass
def queryClosing(self, ev, ownership): pass
def notifyClosing(self, ev):
self.end()
def create_doc(self, ctx):
desktop = ctx.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", ctx)
return desktop.loadComponentFromURL(
"private:factory/swriter", "_blank", 0, ())
def start(self, ctx):
if self.__class__.Out is None:
self.doc = self.create_doc(ctx)
self.doc.addCloseListener(self)
self.__class__.Out = self
self._stdout = sys.stdout
self._stderr = sys.stderr
sys.stdout = self
sys.stderr = self
def end():
klass = WriterAsOutput
if not klass.Out is None:
i = klass.Out
i.doc = None
sys.stdout = i._stdout
sys.stderr = i._stderr
klass.Out = None
else:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
end = staticmethod(end)
def _write(self, s):
if self.doc:
self.doc.getText().getEnd().setString(s)
def write(self, s):
self._write(s)
def start_output(*args):
""" Open writer document as output window. """
WriterAsOutput().start(XSCRIPTCONTEXT.getComponentContext())
def close_output(*args):
""" End to use the document as output window. """
WriterAsOutput.end()
|