def _genRtfFile(questions): """ Given a list of Questions, return an RTF object which contains all data and can be rendered when appropriate using the renderRTF() function in this module. """ doc = rtf.Document() section = rtf.Section() doc.Sections.append(section) section.append("MULTIPLE CHOICE") section.append("") qNum = 1 for question in questions: q, a, ca = _getRTFFormattedContent(question, qNum) q = q.encode('rtfunicode') q = q.replace('\\u9?', '\t') ca = ca.encode('rtfunicode') ca = ca.replace('\\u9?', '\t') section.append(q) for ans in a: ans = ans.encode('rtfunicode') ans = ans.replace('\\u9?', '\t') section.append(ans) section.append(ca) qNum += 1 return doc
def _license_text(self, license_file): """ Generates rich text given a license file-like object :param license_file: file-like object :return: """ wordpad_header = textwrap.dedent(r''' {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset255 Times New Roman;} {\*\generator Riched20 10.0.14393}\viewkind4\uc1 ''').strip().replace('\n', '\r\n') center_space = ' ' r = PyRTF.Renderer() doc = PyRTF.Document() ss = doc.StyleSheet sec = PyRTF.Section() doc.Sections.append(sec) is_blank = False paragraph_text = [''] for line in license_file: if not line or line.isspace(): is_blank = True if is_blank: # first element of paragraph_text is left-aligned, subsequent elements are centered is_centered = False for sec_line in paragraph_text: if is_centered: para_props = PyRTF.ParagraphPS( alignment=PyRTF.ParagraphPS.CENTER) p = PyRTF.Paragraph(ss.ParagraphStyles.Normal, para_props) p.append(sec_line) sec.append(p) elif sec_line: # first element may be nothing, but not whitespace sec.append(sec_line) is_centered = True is_blank = False paragraph_text = [''] if line.startswith(center_space): paragraph_text.append(line.strip()) is_blank = True else: paragraph_text[0] += ' ' + line paragraph_text[0] = paragraph_text[0].strip() f = StringIO() f.write(wordpad_header) r.Write(doc, f) return f.getvalue()
def format_as_rtf(self): try: import PyRTF except ImportError: raise Sorry("The PyRTF module is not available.") doc = PyRTF.Document() ss = doc.StyleSheet section = PyRTF.Section() doc.Sections.append(section) p = PyRTF.Paragraph(ss.ParagraphStyles.Heading2) p.append("Table 1. Data collection and refinement statistics.") section.append(p) n_cols = len(self.columns) + 1 col_widths = [PyRTF.TabPS.DEFAULT_WIDTH * 3] * n_cols table = PyRTF.Table(*col_widths) header = [PyRTF.Cell(PyRTF.Paragraph(""))] anomalous_flag = False for column in self.columns: label = column.label if (column.anomalous_flag): label += "*" anomalous_flag = True column_label = PyRTF.Paragraph(ss.ParagraphStyles.Heading2, label) header.append(PyRTF.Cell(column_label)) table.AddRow(*header) for (stat_name, label, fstring, cif_tag) in keywords: row = [ PyRTF.Cell(PyRTF.Paragraph(ss.ParagraphStyles.Heading2, label)) ] n_none = 0 for column in self.columns: txt = column.format_stat(stat_name) if (txt is None): n_none += 1 p = PyRTF.Paragraph(ss.ParagraphStyles.Normal, PyRTF.ParagraphPS(alignment=2)) p.append(txt) row.append(PyRTF.Cell(p)) if (n_none == len(row) - 1): continue table.AddRow(*row) section.append(table) p = PyRTF.Paragraph(ss.ParagraphStyles.Normal) p.append("Statistics for the highest-resolution shell are shown in " + "parentheses.") section.append(p) return doc
def write_license_rtf(self, rtf_path): # PyRTF and PyRTF-ng it seems do not support Unicode types # when they do this code should read the file using the codecs # module and create a Unicode RTF. if not p.exists(rtf_path): license_file = p.join(self.get_contents_dir(), 'LICENSE.txt') if p.exists(license_file): license_text = open(license_file, 'r').read() else: license_text = 'This software was not shipped with a license.' doc = PyRTF.Document() section = PyRTF.Section() doc.Sections.append(section) for paragraph in re.split("\n\n|\r\n\r\n", license_text): section.append(paragraph) renderer = PyRTF.Renderer() renderer.Write(doc, open(rtf_path, 'w'))
def write_head(self): self.recsection = PyRTF.Section(break_type=PyRTF.Section.PAGE) self.doc.Sections.append(self.recsection) self.add_paragraph("%s\n" % self.r.title, self.ss.ParagraphStyles.Heading1)
def MakeExample3(aves): doc = PyRTF.Document() ss = doc.StyleSheet ht = getFont( u"黑体" ) #PyRTF.Font(r"\'ba\'da\'cc\'e5",'nil',134,2,'02010600030101010101') st = getFont( u"宋体" ) #PyRTF.Font(r"\'cb\'ce\'cc\'e5",'nil',134,2,'02010600030101010101') ss.Fonts.append(ht) ss.Fonts.append(st) section = PyRTF.Section() doc.Sections.append(section) para_props = PyRTF.ParagraphPS(alignment=PyRTF.ParagraphPS.CENTER) # p = PyRTF.Paragraph( PyRTF.UNICODE(u'红外碳硫分析检验记录',size=32,font=ht),para_props) # section.append( p ) text_props = PyRTF.TextPropertySet() text_props.SetFont(ht) text_props.SetSize(32) u = PyRTF.Unicode(u'红外碳硫分析检验记录', text_props) p = PyRTF.Paragraph(u, para_props) section.append(p) p1 = PyRTF.Paragraph("") section.append(p1) # table = PyRTF.Table( 720 * 3, 720 * 1, int(720 * 2.5), int(720 * 2.5), #TabPS.DEFAULT_WIDTH * kd, 720 * 5, alignment=PyRTF.TabPS.CENTER) thin_edge = PyRTF.BorderPS(width=20, style=PyRTF.BorderPS.SINGLE) thick_edge = PyRTF.BorderPS(width=30, style=PyRTF.BorderPS.SINGLE) thin_frame1 = PyRTF.FramePS(thin_edge, thin_edge, thin_edge, thin_edge) thick_frame1 = PyRTF.FramePS(thick_edge, thin_edge, thin_edge, thin_edge) curFrame = thick_frame1 c1 = PyRTF.Cell(PyRTF.Paragraph(u"名称"), curFrame) c1.SetSpan(2) c3 = PyRTF.Cell(PyRTF.Paragraph(u"C%"), curFrame) c4 = PyRTF.Cell(PyRTF.Paragraph(u"S%"), curFrame) #c5 = Cell( Paragraph( str(s.user) ) ) c6 = PyRTF.Cell(PyRTF.Paragraph(u"分析时间"), curFrame) table.AddRow(c1, c3, c4, c6) from PyRTF import Cell, Paragraph, ParagraphPS for ave in aves: i = 0 for s in ave.singles: if i == 0: curFrame = thick_frame1 else: curFrame = thin_frame1 if len(ave.singles) > 1: c1 = Cell(Paragraph(s.name), curFrame) c2 = Cell(Paragraph(str(s.anaxuhao)), curFrame) c3 = Cell(Paragraph("%.5f" % s.c), curFrame) c4 = Cell(Paragraph("%.5f" % s.s), curFrame) #c5 = Cell( Paragraph( str(s.user) ) ) c6 = Cell(Paragraph(str(s.mdate)), curFrame) table.AddRow(c1, c2, c3, c4, c6) else: c1 = Cell(Paragraph(s.name), curFrame) c1.SetSpan(2) c3 = Cell(Paragraph("%.5f" % s.c), curFrame) c4 = Cell(Paragraph("%.5f" % s.s), curFrame) #c5 = Cell( Paragraph( str(s.user) ) ) c6 = Cell(Paragraph(str(s.mdate)), curFrame) table.AddRow(c1, c3, c4, c6) i = i + 1 curFrame = thin_frame1 if len(ave.singles) > 1: c1 = Cell( Paragraph(u"平均值", ParagraphPS(alignment=ParagraphPS.RIGHT)), curFrame) c1.SetSpan(2) #c2 = Cell( Paragraph("") ) c3 = Cell(Paragraph("%.5f" % ave.c), curFrame) c4 = Cell(Paragraph("%.5f" % ave.s), curFrame) #c5 = Cell( Paragraph( str(s.user) ) ) c6 = Cell(Paragraph(""), curFrame) table.AddRow(c1, c3, c4, c6) c1 = Cell( Paragraph(u"标准偏差", ParagraphPS(alignment=ParagraphPS.RIGHT)), curFrame) c1.SetSpan(2) #c2 = Cell( Paragraph("") ) c3 = Cell(Paragraph("%.5f" % ave.cstd), curFrame) c4 = Cell(Paragraph("%.5f" % ave.sstd), curFrame) #c5 = Cell( Paragraph( str(s.user) ) ) c6 = Cell(Paragraph(""), curFrame) table.AddRow(c1, c3, c4, c6) section.append(table) return doc
def exportRtf(self): infoString = QtCore.QString("estrazione anagrafiche in word") reply = QtGui.QMessageBox.question( self, infoString, "Esportare solo le anagrafiche selezionate?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: selected = self.selectSome(self.ui.tabella.selectedItems()) elif reply == QtGui.QMessageBox.No: selected = self.selectAll() else: return filexl = QFileDialog(self) #setto filexl in anymode cioe' ritorna il path del file anche se non esiste filexl.setFileMode(0) # filexl.setNameFilter("word(*.doc *.rtf)") nfile = filexl.getSaveFileName(self, "esporta anagrafiche in word", "", filter="(*.doc *.rtf)") DR = rtf.Renderer() doc = rtf.Document() ss = doc.StyleSheet n = 0 thin_edge = rtf.BorderPropertySet(width=20, style=rtf.BorderPropertySet.SINGLE) thick_edge = rtf.BorderPropertySet(width=80, style=rtf.BorderPropertySet.DOUBLE) mixed_frame = rtf.FramePropertySet(thin_edge, thick_edge, thin_edge, thick_edge) for i in enumerate(selected): section = rtf.Section() table = rtf.Table(rtf.TabPropertySet.DEFAULT_WIDTH * 7, rtf.TabPropertySet.DEFAULT_WIDTH * 3) #section.append(table) img = rtf.Image("pictures/logo.jpg") c1 = rtf.Cell(rtf.Paragraph(img)) c2 = rtf.Cell( rtf.Paragraph( ss.ParagraphStyles.Heading2, "Scheda anagrafica {0}".format(self.activeDb.getNameDb()))) table.AddRow(c1, c2) section.Header.append(table) oggi = datetime.date.today() section.Footer.append( rtf.Paragraph( ss.ParagraphStyles.Normal, "® Copyright Marketing & Telematica mail: [email protected] Data Esportazione: {0}/{1}/{2}" .format(oggi.day, oggi.month, oggi.year))) Id = i[1].data(1).toInt()[0] p = rtf.Paragraph(rtf.ParagraphPS().SetPageBreakBefore(True), '') section.append(p) doc = self.makeRtf(doc, section, Id, i[0] + 1, len((selected))) #doc=self.makeRtf(doc,section) ##print "scrivo",self.OpenFile( 'rtf/vero' ) DR.Write(doc, self.OpenFile(nfile))