Ejemplo n.º 1
0
	def __init__(self, width, columns=1, style='TableGrid', position='last') :
		self._position = position

		self._table = Element().createElement('tbl')
		tblPr = Element().createElement('tblPr')
		style = Element().createElement('tblStyle', attr={'val' : style})
		tblW = Element().createElement('tblW', attr={'w' : str(width), 'type' : 'pct'})

		tblPr.append(style)
		tblPr.append(tblW)
		self._table.append(tblPr)

		#calculate column width
		self._columnWidth = math.floor(width / columns)
		tblGrid = Element().createElement('tblGrid')
		for i in range(columns) :
			tblGrid.append(Element().createElement('gridCol', attr={'w' : str(self._columnWidth)}))

		self._table.append(tblGrid)
Ejemplo n.º 2
0
	def setColumnSize(self, column, size) :
		if column.isdigit() == False :
			newColumn = 0
			if len(column) > 1 :
				newColumn = (len(column) - 1) * 26
			newColumn += (string.ascii_uppercase.index(column[-1:].upper()) + 1)
			column = newColumn
			
		col = Element().createElement('col', attr={'min' : str(column), 'max' : str(column), 'width' : str(size), 'customWidth' : '1'}, prefix='e', attrprefix=None)

		added = False
		for elem in self.xmlString.iter() :
			if elem.tag == '{' + defaults.WPREFIXES['e'] + '}cols' :
				elem.append(col)
				added = True

		if added == False :
			cols = Element().createElement('cols', prefix='e')
			cols.append(col)

			for elem in self.xmlString.iter() :
				if elem.tag == '{' + defaults.WPREFIXES['e'] + '}sheetFormatPr' :
					elem.addnext(cols)
Ejemplo n.º 3
0
	def addRow(self, val, style='default') :
		row = self._getNewRow()

		for v in val :
			column = self._getNewColumn(style)
			
			textEl = Element().createElement('t')
			textEl.text = v
			para = Element().createElement('p')
			run = Element().createElement('r')
			run.append(textEl)
			para.append(run)
			column.append(para)
			
			row.append(column)

		self._table.append(row)
Ejemplo n.º 4
0
	def addData(self, column, value, type='number') :
		for elem in self.xmlString.iter() :
			if elem.tag == '{' + defaults.WPREFIXES['e'] + '}sheetData' :
				#get row number
				for rowNum in column :
					if rowNum.isdigit() :
						rowAlreadyExists = False
						#check if row already in file
						for item in self.xmlString.iter() :
							if item.tag == '{' + defaults.WPREFIXES['e'] + '}' + 'row' :
								if item.attrib['r'] == rowNum :
									rowAlreadyExists = True
									row = item

						if rowAlreadyExists == False :
							row = Element().createElement('row', attr={'r' : str(rowNum)}, prefix='e', attrprefix=None)
						
						if type not in self.types :
							type = 'number'
						c = Element().createElement('c', attr={'r' : column, 't' : self.types[type]}, prefix='e', attrprefix=None)
						
						if type == 'number' :
							v = Element().createElement('v', prefix=None)
							v.text = value
							c.append(v)
						
						if type == 'text' :
							isEl = Element().createElement('is', prefix='e')
							tEl = Element().createElement('t', prefix='e')
							tEl.text = value
							isEl.append(tEl)
							c.append(isEl)

						if type == 'formule' :
							f = Element().createElement('f', prefix='e')
							f.text = value
							c.append(f)

						row.append(c)
						elem.append(row)
Ejemplo n.º 5
0
class Table :

	_table = ''
	_columnWidth = ''
	_position = ''

	def __init__(self, width, columns=1, style='TableGrid', position='last') :
		self._position = position

		self._table = Element().createElement('tbl')
		tblPr = Element().createElement('tblPr')
		style = Element().createElement('tblStyle', attr={'val' : style})
		tblW = Element().createElement('tblW', attr={'w' : str(width), 'type' : 'pct'})

		tblPr.append(style)
		tblPr.append(tblW)
		self._table.append(tblPr)

		#calculate column width
		self._columnWidth = math.floor(width / columns)
		tblGrid = Element().createElement('tblGrid')
		for i in range(columns) :
			tblGrid.append(Element().createElement('gridCol', attr={'w' : str(self._columnWidth)}))

		self._table.append(tblGrid)

	def addRow(self, val, style='default') :
		row = self._getNewRow()

		for v in val :
			column = self._getNewColumn(style)
			
			textEl = Element().createElement('t')
			textEl.text = v
			para = Element().createElement('p')
			run = Element().createElement('r')
			run.append(textEl)
			para.append(run)
			column.append(para)
			
			row.append(column)

		self._table.append(row)

	def get(self) :
		return self._table

	def getPosition(self) :
		return self._position

	def _getNewRow(self) :
		return Element().createElement('tr')

	def _getNewColumn(self, style) :
		column = Element().createElement('tc')
		tcPr = Element().createElement('tcPr')
		tcPr.append(Element().createElement('tcW', attr={'w' : str(self._columnWidth), 'type' : 'dxa'}))

		if style == 'dashed' :
			tcBorders = Element().createElement('tcBorders')
			attr = {'val' : 'dashed', 'sz' : '4', 'space' : '0', 'color' : 'auto'}
			top = Element().createElement('top', attr=attr)
			left = Element().createElement('left', attr=attr)
			bottom = Element().createElement('bottom', attr=attr)
			right = Element().createElement('right', attr=attr)
			tcBorders.append(top)
			tcBorders.append(left)
			tcBorders.append(bottom)
			tcBorders.append(right)
			tcPr.append(tcBorders)

		column.append(tcPr)

		return column
Ejemplo n.º 6
0
	def _getNewColumn(self, style) :
		column = Element().createElement('tc')
		tcPr = Element().createElement('tcPr')
		tcPr.append(Element().createElement('tcW', attr={'w' : str(self._columnWidth), 'type' : 'dxa'}))

		if style == 'dashed' :
			tcBorders = Element().createElement('tcBorders')
			attr = {'val' : 'dashed', 'sz' : '4', 'space' : '0', 'color' : 'auto'}
			top = Element().createElement('top', attr=attr)
			left = Element().createElement('left', attr=attr)
			bottom = Element().createElement('bottom', attr=attr)
			right = Element().createElement('right', attr=attr)
			tcBorders.append(top)
			tcBorders.append(left)
			tcBorders.append(bottom)
			tcBorders.append(right)
			tcPr.append(tcBorders)

		column.append(tcPr)

		return column
Ejemplo n.º 7
0
	def __init__(self, text='', style='NormalWeb', styles=None) :
	
		self._styles = {
			'bold' : False,
			'italic' : False,
			'underline' : False,
			'uppercase' : False,
			'color' : False,
			'font' : False,
			'size' : False
		}

		if styles is not None :
			for key, value in styles.items() :
				self._styles[key] = value

		#root element
		self.para = Element().createElement('p')
		
		#style element
		pPr = Element().createElement('pPr')
		self.para.append(pPr)
		pPr.append(Element().createElement('pStyle', attr={'val' : style}))

		#run and text element
		run = Element().createElement('r')
		self._prop = Element().createElement('rPr')
		run.append(self._prop)

		#set styles
		self.setStyles()
		
		textEl = Element().createElement('t', attr={'space' : 'preserve'})
		textEl.text = text
		run.append(textEl)
		self.para.append(run)
Ejemplo n.º 8
0
class Paragraph :
	
	_prop = ''
	
	def __init__(self, text='', style='NormalWeb', styles=None) :
	
		self._styles = {
			'bold' : False,
			'italic' : False,
			'underline' : False,
			'uppercase' : False,
			'color' : False,
			'font' : False,
			'size' : False
		}

		if styles is not None :
			for key, value in styles.items() :
				self._styles[key] = value

		#root element
		self.para = Element().createElement('p')
		
		#style element
		pPr = Element().createElement('pPr')
		self.para.append(pPr)
		pPr.append(Element().createElement('pStyle', attr={'val' : style}))

		#run and text element
		run = Element().createElement('r')
		self._prop = Element().createElement('rPr')
		run.append(self._prop)

		#set styles
		self.setStyles()
		
		textEl = Element().createElement('t', attr={'space' : 'preserve'})
		textEl.text = text
		run.append(textEl)
		self.para.append(run)
	
	def setStyles(self) :
		if self._styles['bold'] is not False :
			self._prop.append(Element().createElement('b', attr={'val' : 'true'}))
		if self._styles['italic'] is not False :
			self._prop.append(Element().createElement('i', attr={'val' : 'true'}))
		if self._styles['underline'] is not False :
			if self._styles['underline'] in defaults.colors :
				self._prop.append(Element().createElement('u', attr={'val' : 'single', 'color' : defaults.colors[self._styles['color']].replace('#', '')}))
			else :
				if self._styles['underline'] == True :
					underline = '#000000'
				self._prop.append(Element().createElement('u', attr={'val' : 'single', 'color' : underline.replace('#', '')}))
		if self._styles['uppercase'] is not False :
			self._prop.append(Element().createElement('caps', attr={'val' : 'true'}))
		if self._styles['color'] is not False :
			if self._styles['color'] in defaults.colors :
				self._prop.append(Element().createElement('color', attr={'val' : defaults.colors[self._styles['color']].replace('#', '')}))
			else :
				self._prop.append(Element().createElement('color', attr={'val' : self._styles['color'].replace('#', '')}))
		if self._styles['font'] is not False :
			self._prop.append(Element().createElement('rFonts', attr={'ascii' : self._styles['font'], 'hAnsi' : self._styles['font']}))
		if self._styles['size'] is not False :
			self._prop.append(Element().createElement('sz', attr={'val' : str(self._styles['size'] * 2)}))

	def get(self) :
		return self.para