def __init__(self, file_name, **kwargs): fin = file(file_name, 'rb') pict_type = self.PICT_TYPES[file_name[-3:].lower()] if pict_type == self.PNG_LIB: width, height = _get_png_dimensions(fin.read(100)) else: width, height = _get_jpg_dimensions(fin) codes = [ pict_type, 'picwgoal%s' % (width * 20), 'pichgoal%s' % (height * 20) ] for kwarg, code, default in [('scale_x', 'scalex', '100'), ('scale_y', 'scaley', '100'), ('crop_left', 'cropl', '0'), ('crop_right', 'cropr', '0'), ('crop_top', 'cropt', '0'), ('crop_bottom', 'cropb', '0')]: codes.append('pic%s%s' % (code, kwargs.pop(kwarg, default))) # reset back to the start of the file to get all of it and now # turn it into hex. fin.seek(0, 0) data = [] image = hexlify(fin.read()) for i in range(0, len(image), 128): data.append(image[i:i + 128]) data = r'{\pict{\%s}%s}' % ('\\'.join(codes), '\n'.join(data)) RawCode.__init__(self, data)
def __init__( self, file_name, fin=None, **kwargs ) : if fin is None: fin = file( file_name, 'rb' ) pict_type = self.PICT_TYPES[ file_name[ -3 : ].lower() ] if pict_type == self.PNG_LIB : width, height = _get_png_dimensions( fin.read( 100 ) ) else : width, height = _get_jpg_dimensions( fin ) codes = [ pict_type, 'picwgoal%s' % (width * 20), 'pichgoal%s' % (height * 20) ] for kwarg, code, default in [ ( 'scale_x', 'scalex', '100' ), ( 'scale_y', 'scaley', '100' ), ( 'crop_left', 'cropl', '0' ), ( 'crop_right', 'cropr', '0' ), ( 'crop_top', 'cropt', '0' ), ( 'crop_bottom', 'cropb', '0' ) ] : codes.append( 'pic%s%s' % ( code, kwargs.pop( kwarg, default ) ) ) # reset back to the start of the file to get all of it and now # turn it into hex. fin.seek( 0, 0 ) data = [] image = hexlify( fin.read() ) for i in range( 0, len( image ), 128 ) : data.append( image[ i : i + 128 ] ) data = r'{\pict{\%s}%s}' % ( '\\'.join( codes ), '\n'.join( data ) ) RawCode.__init__( self, data )
def __init__(self, file_name, **kwargs): img_info = rtfng.misc.image_utils.get_image_info_from_file(file_name) if not img_info: pass width, height, pict_type = img_info pict_type = self.PICT_TYPES[pict_type] if "fixed_width" in kwargs: w = kwargs["fixed_width"] else: w = width * 10 h = (w * height // width) codes = [pict_type, 'picwgoal%s' % w, 'pichgoal%s' % h] for kwarg, code, default in [('scale_x', 'scalex', '100'), ('scale_y', 'scaley', '100'), ('crop_left', 'cropl', '0'), ('crop_right', 'cropr', '0'), ('crop_top', 'cropt', '0'), ('crop_bottom', 'cropb', '0')]: codes.append('pic%s%s' % (code, kwargs.pop(kwarg, default))) # Reset back to the start of the file to get all of it and now # turn it into hex. with open(file_name, 'rb') as fin: fin.seek(0, 0) data = [] image = hexlify(fin.read()).decode("ascii") for i in range(0, len(image), 128): data.append(image[i:i + 128]) data = r'{\pict{\%s}%s}' % ('\\'.join(codes), '\n'.join(data)) RawCode.__init__(self, data)
def make_pictures(): doc, section, styles = RTFTestCase.initializeDoc() # text can be added directly to the section a paragraph object is create as needed section.append( 'Image Example 1' ) section.append( 'You can add images in one of two ways, either converting the ' 'image each and every time like;' ) image = Image( 'examples/image.jpg' ) section.append( Paragraph( image ) ) section.append( 'Or you can use the image object to convert the image and then ' 'save it to a raw code element that can be included later.' ) # Test RawCode -- split into separate test? rawCodeDecl = image.ToRawCode('TEST_IMAGE') assert rawCodeDecl.startswith('TEST_IMAGE = RawCode( """') assert rawCodeDecl.endswith('""" )') rawCode = RawCode(image.Data) section.append(rawCode) section.append('The above picture was displayed from a RawCode object without a Paragraph wrapper.') section.append( 'Here are some png files' ) for f in [ 'examples/img1.png', 'examples/img2.png', 'examples/img3.png', 'examples/img4.png' ] : section.append( Paragraph( Image( f ) ) ) return doc
def __init__( self, file_name, **kwargs ) : fin = file( file_name, 'rb' ) pict_type = self.PICT_TYPES[ file_name[ -3 : ].lower() ] if pict_type == self.PNG_LIB : width, height = _get_png_dimensions( fin.read( 100 ) ) else : width, height = _get_jpg_dimensions( fin ) # Find scale ratio to fit an image in a page # 210mmx297mm - standard A4 page size # TODO: What to do with different page sizes? scale_factor = 2.2 width_base = 210 * scale_factor height_base = 297 * scale_factor width_s, height_s = width / width_base, height / height_base scale = max((width_s, height_s)) if scale < 1: scale = 1 codes = [ pict_type, 'picwgoal%d' % (width * 20 / scale), 'pichgoal%d' % (height * 20 / scale) ] for kwarg, code, default in [ ( 'scale_x', 'scalex', '100' ), ( 'scale_y', 'scaley', '100' ), ( 'crop_left', 'cropl', '0' ), ( 'crop_right', 'cropr', '0' ), ( 'crop_top', 'cropt', '0' ), ( 'crop_bottom', 'cropb', '0' ) ] : codes.append( 'pic%s%s' % ( code, kwargs.pop( kwarg, default ) ) ) # reset back to the start of the file to get all of it and now # turn it into hex. fin.seek( 0, 0 ) data = [] image = hexlify( fin.read() ) for i in range( 0, len( image ), 128 ) : data.append( image[ i : i + 128 ] ) data = r'{\pict{\%s}%s}' % ( '\\'.join( codes ), '\n'.join( data ) ) RawCode.__init__( self, data )
def __init__(self, file_name, **kwargs): # Try to import PIL. try: from PIL import Image except: raise Exception('Unable to import PIL Image library') # Calculate size of image. image = Image.open(file_name) self.width, self.height = image.size # Generate header codes. pict_type = self.PICT_TYPES[file_name[-3:].lower()] codes = [ pict_type, 'picwgoal%s' % (self.width * 20), 'pichgoal%s' % (self.height * 20) ] for kwarg, code, default in [('scale_x', 'scalex', '100'), ('scale_y', 'scaley', '100'), ('crop_left', 'cropl', '0'), ('crop_right', 'cropr', '0'), ('crop_top', 'cropt', '0'), ('crop_bottom', 'cropb', '0')]: codes.append('pic%s%s' % (code, kwargs.pop(kwarg, default))) # Reset back to the start of the file to get all of it and now # turn it into hex. # I tried using image.getdata() below but it relies on having libjpeg installed. fin = file(file_name, 'rb') fin.seek(0, 0) data = [] image = hexlify(fin.read()) for i in range(0, len(image), 128): data.append(image[i:i + 128]) data = r'{\pict{\%s}%s}' % ('\\'.join(codes), '\n'.join(data)) RawCode.__init__(self, data)
def make_charInline(): doc, section, styles = RTFTestCase.initializeDoc() p = Paragraph() p.append(Inline('Simple Inline Element')) section.append(p) # Test various element types inside Inline element. p = Paragraph() p.append( Inline('First Inline Element', TAB, 'Second Inline Element', RawCode(r'\tab '), 'After tab')) section.append(p) return doc
def __init__( self, file_name, **kwargs ) : # Try to import PIL. try: from PIL import Image except: raise Exception('Unable to import PIL Image library') # Calculate size of image. image = Image.open(file_name) self.width, self.height = image.size # Generate header codes. pict_type = self.PICT_TYPES[ file_name[ -3 : ].lower() ] codes = [ pict_type, 'picwgoal%s' % (self.width * 20), 'pichgoal%s' % (self.height * 20) ] for kwarg, code, default in [ ( 'scale_x', 'scalex', '100' ), ( 'scale_y', 'scaley', '100' ), ( 'crop_left', 'cropl', '0' ), ( 'crop_right', 'cropr', '0' ), ( 'crop_top', 'cropt', '0' ), ( 'crop_bottom', 'cropb', '0' ) ] : codes.append( 'pic%s%s' % ( code, kwargs.pop( kwarg, default ) ) ) # Reset back to the start of the file to get all of it and now # turn it into hex. # I tried using image.getdata() below but it relies on having libjpeg installed. fin = file(file_name, 'rb') fin.seek( 0, 0 ) data = [] image = hexlify( fin.read() ) for i in range( 0, len( image ), 128 ) : data.append( image[ i : i + 128 ] ) data = r'{\pict{\%s}%s}' % ( '\\'.join( codes ), '\n'.join( data ) ) RawCode.__init__( self, data )
def __init__( self, file_name, **kwargs ) : im = PIL_Image.open(file_name) im.thumbnail((500,500), PIL_Image.ANTIALIAS) width = im.width height = im.height # if image not in supported format try to convert it file_format = im.format.lower() if file_format not in self.PICT_TYPES: file_format = 'png' im.save(file_name, file_format.upper()) pict_type = self.PICT_TYPES[file_format] im.close() codes = [ pict_type, 'picwgoal%s' % (width * 20), 'pichgoal%s' % (height * 20) ] for kwarg, code, default in [ ( 'scale_x', 'scalex', '100' ), ( 'scale_y', 'scaley', '100' ), ( 'crop_left', 'cropl', '0' ), ( 'crop_right', 'cropr', '0' ), ( 'crop_top', 'cropt', '0' ), ( 'crop_bottom', 'cropb', '0' ) ] : codes.append( 'pic%s%s' % ( code, kwargs.pop( kwarg, default ) ) ) # reset back to the start of the file to get all of it and now # turn it into hex. fin = open(file_name, 'rb') data = [] image = hexlify( fin.read() ) for i in range( 0, len( image ), 128 ) : data.append( image[ i : i + 128 ] ) data = r'{\pict{\%s}%s}' % ( '\\'.join( codes ), '\n'.join([x.decode('UTF-8') for x in data])) RawCode.__init__( self, data )
(2, TabPropertySet.DEFAULT_WIDTH * 2), (3, TabPropertySet.DEFAULT_WIDTH * 3)]: indent = TabPropertySet.DEFAULT_WIDTH ps = ParagraphStyle('List %s' % idx, TextStyle(TextPropertySet(result.Fonts.Arial, 22)), ParagraphPropertySet( space_before=60, space_after=60, first_line_indent=-indent, left_indent=indent)) result.ParagraphStyles.append(ps) return result PAGE_NUMBER = RawCode(r'{\field{\fldinst page}}') TOTAL_PAGES = RawCode(r'{\field{\fldinst numpages}}') SECTION_PAGES = RawCode(r'{\field{\fldinst sectionpages}}') ARIAL_BULLET = RawCode(r'{\f2\'95}') class Document: def __init__(self, style_sheet=None, default_language=None, view_kind=None, view_zoom_kind=None, view_scale=None): self.StyleSheet = style_sheet or MakeDefaultStyleSheet() self.Sections = AttributedList(Section)