def simpledoc(noimagecopy=False): """Make a docx (document, relationships) for use in other docx tests""" relationships = relationshiplist() imagefiledict = {} document = newdocument() docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0] docbody.append(heading('Heading 1', 1)) docbody.append(heading('Heading 2', 2)) docbody.append(paragraph('Paragraph 1')) for point in ['List Item 1', 'List Item 2', 'List Item 3']: docbody.append(paragraph(point, style='ListNumber')) docbody.append(pagebreak(type='page')) docbody.append(paragraph('Paragraph 2')) docbody.append( table([['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']])) docbody.append(pagebreak(type='section', orient='portrait')) if noimagecopy: relationships, picpara, imagefiledict = picture( relationships, IMAGE1_FILE, 'This is a test description', imagefiledict=imagefiledict) else: relationships, picpara = picture(relationships, IMAGE1_FILE, 'This is a test description') docbody.append(picpara) docbody.append(pagebreak(type='section', orient='landscape')) docbody.append(paragraph('Paragraph 3')) if noimagecopy: return (document, docbody, relationships, imagefiledict) else: return (document, docbody, relationships)
def create_header(self): """ Create the header information """ # Add logo self.relationships, picpara = docx.picture(self.relationships, 'curoseven_630x158.png', 'logo') self.body.append(picpara) self.body.append(docx.heading("Bespoke Blood Analysis", 1)) self.body.append(docx.heading("Report & Recommendation", 1))
def output_docx_glossary(glos, docxfilename): import docx # requires https://github.com/mikemaccana/python-docx items = glos.items() items.sort() relationships = docx.relationshiplist() document = docx.newdocument() docbody = document.xpath('/w:document/w:body', namespaces=docx.nsprefixes)[0] docbody.append(docx.heading("ONTORULE glossary", 1)) for k,v in items: docbody.append(docx.heading(k, 2)) docbody.append(docx.paragraph(to_docx(v))) coreprops = docx.coreproperties(title='ONTORULE glossary', subject="", creator='ONTORULE', keywords=[]) appprops = docx.appproperties() contenttypes = docx.contenttypes() websettings = docx.websettings() wordrelationships = docx.wordrelationships(relationships) docx.savedocx(document,coreprops,appprops,contenttypes,websettings,wordrelationships, docxfilename)
def depart_title(self, node): dprint() text = ''.join(self.states.pop()) dprint(_func='* heading', text=repr(text), level=self.sectionlevel) self.docbody.append(docx.heading(text, self.sectionlevel))
for j in range(i,n): if (A[i][j]==1): A[j][i]=1 # if edge exists => add backward mark in A matrix r+=1 # increase number of edges graph.add_edge(node[i],node[j]) # add an adge for drawing graph.layout(yapgvb.engines.dot) # 'Dot' fits the best format = yapgvb.formats.png filename = 'graph.%s' % format print " Rendering %s ..." % filename graph.render(filename) # Draws a graph relationships = docx.relationshiplist() document = docx.newdocument() docbody = document.xpath('/w:document/w:body', namespaces=docx.nsprefixes)[0] docbody.append(heading(u'''6. Найти инварианты графа, заданного матрицей смежности ''',1) ) # Create and insert basic table containing input info Table_Basic=[] for i in range(n+1): Table_Basic.append([]) for j in range(n+1): Table_Basic[i].append("") for i in range(1,n+1): Table_Basic[0][i] = "x"+str(i) Table_Basic[i][0] = "x"+str(i) for i in range(1,n+1): for j in range(1, n+1): Table_Basic[i][j]=str(A[i-1][j-1]) docbody.append(table(Table_Basic,False,borders={'all':{'color':'auto','space':1,'sz':1}}))
def append_heading(self, string, level=1): self.body.append(docx.heading(string, level))
def generate_docx_resume(): relationships = docx.relationshiplist() document = docx.newdocument() body = document.xpath('/w:document/w:body', namespaces=docx.nsprefixes)[0] data = resume_dict() body.append(docx.heading('Resume of', 3)) body.append(docx.heading(data['resume'].name, 1)) body.append(docx.heading(data['resume'].email, 2)) body.append(docx.heading(data['resume'].url, 2)) body.append(docx.heading('PROFILE SUMMARY', 2)) for entry in data['profile_entries']: body.append(docx.paragraph(entry.entry, style='ListBullet')) body.append(docx.heading('LINKS', 2)) for link in data['important_links']: body.append(docx.paragraph('%s - %s' % ( link.description, link.url, ), style='ListBullet')) body.append(docx.heading('EXPERTISE', 2)) for entry in data['expertise_entries']: body.append(docx.paragraph(entry.entry, style='ListBullet')) # experience ret += 'EXPERIENCE'.ljust(text_width, '.') + '\n' ret += '\n' for entry in data['work_history_entries']: ret += ' ' + entry.client_name.upper() + '\n' ret += ' ' + entry.location + '\n' ret += ' ' + entry.timespan + '\n' ret += ' ' + entry.title + '\n' ret += '\n' for achievement in entry.workachievement_set.all(): wrapped = textwrap.fill( achievement.description, text_width-6, subsequent_indent=' ', ) ret += ' ' + wrapped + '\n' ret += '\n' ret += '\n\n' # contributions ret += 'COMMUNITY CONTRIBUTIONS'.ljust(text_width, '.') + '\n' ret += '\n' for project in data['projects']: ret += ' ' + project.name.upper() + '\n' wrapped = textwrap.fill( strip_tags(project.short_description), text_width-3, subsequent_indent=' ', ) ret += ' ' + wrapped + '\n' wrapped = textwrap.fill( strip_tags(project.long_description), text_width-3, subsequent_indent=' ', ) ret += ' ' + wrapped + '\n\n' if project.deployment_url: ret += ' Project Page: %s\n' % project.deployment_url ret += ' Source Code: %s\n' % project.src_url ret += '\n\n' tag_line_top = "This is my plain text resume generator" tag_line_middle = "Outputting at %s columns" % text_width tag_line_bottom = "http://github.com/ben174/bugben" ret += '-' * text_width + '\n' ret += tag_line_top.center(text_width) + '\n' ret += tag_line_middle.center(text_width) + '\n' ret += tag_line_bottom.center(text_width) + '\n' ret += '-' * text_width + '\n' # Create our properties, contenttypes, and other support files title = 'Resume of %s' % data['resume'].name subject = 'My awesome resume!' creator = data['resume'].name keywords = [] coreprops = docx.coreproperties( title=title, subject=subject, creator=creator, keywords=keywords, ) appprops = docx.appproperties() contenttypes = docx.contenttypes() websettings = docx.websettings() wordrelationships = docx.wordrelationships(relationships) data = None with tempfile.NamedTemporaryFile() as temp: docx.savedocx( document, coreprops, appprops, contenttypes, websettings, wordrelationships, temp.name ) data = open(temp.name).read() return data
from copy import deepcopy from tempfile import TemporaryFile, mkdtemp import docx as dx if __name__ == '__main__': # Default set of relationshipships - these are the minimum components of a document relationships = dx.getRelationships() # Make a new document tree - this is the main part of a Word document document = dx.newdocument() # This xpath location is where most interesting content lives docbody = document.xpath('/w:document/w:body', namespaces=dx.nsprefixes)[0] # Append two headings and a paragraph docbody.append(dx.heading('''Welcome to Python's docx module''', 1)) docbody.append( dx.heading('Make and edit docx in 200 lines of pure Python', 2)) docbody.append( dx.paragraph( 'The module was created when I was looking for a Python support for MS Word .doc files on PyPI and Stackoverflow. Unfortunately, the only solutions I could find used:' )) # Add a numbered list for point in [ '''COM automation''', '''.net or Java''', '''Automating OpenOffice or MS Office''' ]: docbody.append(dx.paragraph(point, style='ListNumber')) docbody.append( dx.paragraph(
from copy import deepcopy from tempfile import TemporaryFile, mkdtemp import docx as dx if __name__ == '__main__': # Default set of relationshipships - these are the minimum components of a document relationships = dx.getRelationships() # Make a new document tree - this is the main part of a Word document document = dx.newdocument() # This xpath location is where most interesting content lives docbody = document.xpath('/w:document/w:body', namespaces=dx.nsprefixes)[0] # Append two headings and a paragraph docbody.append(dx.heading('''Welcome to Python's docx module''',1) ) docbody.append(dx.heading('Make and edit docx in 200 lines of pure Python',2)) docbody.append(dx.paragraph('The module was created when I was looking for a Python support for MS Word .doc files on PyPI and Stackoverflow. Unfortunately, the only solutions I could find used:')) # Add a numbered list for point in ['''COM automation''','''.net or Java''','''Automating OpenOffice or MS Office''']: docbody.append(dx.paragraph(point,style='ListNumber')) docbody.append(dx.paragraph('''For those of us who prefer something simpler, I made docx.''')) docbody.append(dx.heading('Making documents',2)) docbody.append(dx.paragraph('''The docx module has the following features:''', font='Arial', fontsize=10)) # Add some bullets for point in ['Paragraphs','Bullets','Numbered lists','Multiple levels of headings','Tables','Document Properties']: docbody.append(dx.paragraph(point,style='ListBullet'))
<h1>PDF Demo</h1> <p>lala</p> <h2>h2</h2> </body> </html> """ result = StringIO.StringIO() pdf = pisa.CreatePDF(StringIO.StringIO(html), result) f = open('generated.pdf', 'w') f.write(result.getvalue()) import odf.opendocument import odf.text textdoc = odf.opendocument.OpenDocumentText() p = odf.text.P(text="Hello World!") textdoc.text.addElement(p) textdoc.save("generated", True) import docx document = docx.newdocument() docbody = document.xpath('/w:document/w:body', namespaces=docx.nsprefixes)[0] docbody.append(docx.heading('''Welcome to Python's docx module''',1) ) docbody.append(docx.heading('Make and edit docx in 200 lines of pure Python',2)) docbody.append(docx.paragraph('The module was created when I was looking for a Python support for MS Word .doc files')) coreprops = docx.coreproperties(title='Python docx demo',subject='A practical example of making docx from Python',creator='stas',keywords=['python','Office Open XML','Word']) appprops = docx.appproperties() contenttypes = docx.contenttypes() websettings = docx.websettings() wordrelationships = docx.wordrelationships(docx.relationshiplist()) docx.savedocx(document, coreprops, appprops, contenttypes, websettings, wordrelationships, 'generated.docx')
run.append(t) p.append(run) add_hyperlink(p, (refworksreportlinkprefix if (refid and refid < 10000) else refworkslinkprefix) + row["ID"]) doc_body.append(p) return p doc = docx.newdocument() doc_body = None for el in doc.iter(): if el.tag == '{' + docx.nsprefixes['w'] + '}body': doc_body = el break doc_body.append(docx.heading(title + " (" + str(len(outvals)) + ")", 1)) doc_body.append( docx.paragraph([ "This report contains all publication types except for \"" + (excludegenres[0] if len(excludegenres) <= 1 else "\"; \"".join( [excludegenres[k] for k in range(len(excludegenres) - 1)]) + "\" and \"" + excludegenres[len(excludegenres) - 1]) + "\"" + (", as well as publications published before " + str(yearmin) if yearmin else "") + ".", '' ])) doc_body.append(docx.heading("Statistics", 2)) stats_table = [] stats_table.append([ "", "Articles" + u" \u2013 " + rkey, "Articles" + u" \u2013 " + nrkey, "Others", "TOTAL" ])
import docx document = docx.heading('Document Title', 0) p = document.add_paragraph("A plain paragraph having some") document.add_page_break() document.save('demo.docx') docx.heading('Document Title', 0)
def simpledoc(noimagecopy=False): """Make a docx (document, relationships) for use in other docx tests""" relationships = relationshiplist() imagefiledict = {} document = newdocument() docbody = document.xpath("/w:document/w:body", namespaces=nsprefixes)[0] docbody.append(heading("Heading 1", 1)) docbody.append(heading("Heading 2", 2)) docbody.append(paragraph("Paragraph 1")) for point in ["List Item 1", "List Item 2", "List Item 3"]: docbody.append(paragraph(point, style="ListNumber")) docbody.append(pagebreak(type="page")) docbody.append(paragraph("Paragraph 2")) docbody.append(table([["A1", "A2", "A3"], ["B1", "B2", "B3"], ["C1", "C2", "C3"]])) docbody.append(pagebreak(type="section", orient="portrait")) if noimagecopy: relationships, picpara, imagefiledict = picture( relationships, IMAGE1_FILE, "This is a test description", imagefiledict=imagefiledict ) else: relationships, picpara = picture(relationships, IMAGE1_FILE, "This is a test description") docbody.append(picpara) docbody.append(pagebreak(type="section", orient="landscape")) docbody.append(paragraph("Paragraph 3")) if noimagecopy: return (document, docbody, relationships, imagefiledict) else: return (document, docbody, relationships)