#!/usr/bin/env python
"""
Making PyRTF be russian-aware ;-)
"""
# PyRuRTF.py
#
# This library is free software; you can redistribute
# it and/or modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# (c) 2008, Yury Yurevich (http://www.pyobject.ru)
from PyRTF import *
russianFontsAreSet = False
def setRussianFonts():
global russianFontsAreSet
if not russianFontsAreSet:
for font in StandardFonts:
if font.Name != 'Symbol':
font.SetCharacterSet(204)
russianFontsAreSet = True
class RussianDocument(Document):
def __init__(self, *args, **kwargs):
if 'default_language' not in kwargs and len(args) < 3:
# default_language is a third positional arg,
# so if it is in args, we don't want to override it
kwargs['default_language'] = Languages.Russian
setRussianFonts()
Document.__init__(self, *args, **kwargs)
class RussianRenderer(Renderer):
def _WriteDocument(self) :
settings = Settings()
assert Languages.IsValid(self._doc.DefaultLanguage)
assert ViewKind.IsValid(self._doc.ViewKind)
assert ViewZoomKind.IsValid(self._doc.ViewZoomKind)
assert ViewScale.IsValid(self._doc.ViewScale)
settings.append(self._doc.DefaultLanguage, 'deflang%s')
settings.append(self._doc.ViewKind , 'viewkind%s')
settings.append(self._doc.ViewZoomKind, 'viewzk%s')
settings.append(self._doc.ViewScale, 'viewscale%s')
self._write("{\\rtf1\\ansi\\ansicpg1251\\deff0%s\n" % settings)
#!/usr/bin/python
# encoding: cp1251
# PyRyRTF (http://dumpz.org/2013/) example
import PyRuRTF as rtf
def make_doc():
doc = rtf.RussianDocument()
style = doc.StyleSheet
section = rtf.Section()
doc.Sections.append(section)
section.append('Пример 1')
section.append('')
section.append( 'Проверка примера'
'Используется 8-битный текст в кодировке cp1251.')
return doc
def save_doc(filename):
doc = make_doc()
renderer = rtf.RussianRenderer()
renderer.Write(doc, open(filename, 'w'))
if __name__ == '__main__':
save_doc('fee.rtf')
Заранее благодарен…