Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from docx import Document
- document = Document()
- # Add header and footer on all pages
- document.save("demo.docx")
- from docx import Document
- import win32com.client as win32
- import os.path
- import tempfile
- tempdir = tempfile.gettempdir()
- msword = win32.gencache.EnsureDispatch('Word.Application')
- tempfile = os.path.join(tempdir, "temp.doc")
- document = Document()
- document.save(tempfile)
- doc = msword.Documents.Open(tempfile)
- doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
- doc.Sections(1).Footers(1).PageNumbers.Add()
- doc.SaveAs(tempfile, FileFormat = 0)
- document = Document(tempfile)
- from docx import Document
- document = Document()
- document.add_heading('Document Title', 0)
- p = document.add_paragraph('A plain paragraph having some ')
- p.add_run('bold').bold = True
- p.add_run(' and some ')
- p.add_run('italic.').italic = True
- document.add_heading('Heading, level 1', level=1)
- document.add_paragraph('Intense quote', style='IntenseQuote')
- document.add_paragraph(
- 'first item in unordered list', style='ListBullet'
- )
- document.add_paragraph(
- 'first item in ordered list', style='ListNumber'
- )
- document.save('demo.docx')
- from docx import Document
- document = Document('demo.docx')
- document.add_heading('A New Title for my Document', 0)
- p = document.add_paragraph('A new paragraph having some plain ')
- p.add_run('bold').bold = True
- p.add_run(' and some ')
- p.add_run('italic.').italic = True
- document.add_heading('New Heading, level 1', level=1)
- document.add_paragraph('Intense quote', style='IntenseQuote')
- document.add_paragraph(
- 'first new item in unordered list', style='ListBullet'
- )
- document.add_paragraph(
- 'first new item in ordered list', style='ListNumber'
- )
- document.save('demo.docx')
- from docx import Document
- document = Document('demo.docx')
- document.add_page_break()
- recordset = [ [1, "101", "Spam"], [2, "42", "Eggs"], [3, "631", "Spam, spam, eggs, and spam"]]
- table = document.add_table(rows=1, cols=3)
- hdr_cells = table.rows[0].cells
- hdr_cells[0].text = 'Qty'
- hdr_cells[1].text = 'Id'
- hdr_cells[2].text = 'Desc'
- for item in recordset:
- row_cells = table.add_row().cells
- row_cells[0].text = str(item[0])
- row_cells[1].text = str(item[1])
- row_cells[2].text = item[2]
- table.style = 'ColorfulShading'
- document.save('demo.docx')
- import shutil
- shutil.copyfile('demo.docx', 'demo_copy.docx')
- import win32com.client as win32
- msword = win32.gencache.EnsureDispatch('Word.Application')
- doc = msword.Documents.Add
- doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
- doc.Sections(1).Footers(1).PageNumbers.Add()
- from docx import Document
- document = Document("template.docx")
- # Do your editing
- document.save("demo.docx")
- unzip template.docx
- import zipfile
- document = zipfile.ZipFile("template.docx")
- for xml in document.filelist:
- if "header" in xml.filename:
- read = document.read(xml.filename)
- print(read.decode())
- <w:r><w:t>ThisIsMyHeader</w:t></w:r>
- from docx import Document
- document = Document()
- header = document.sections[0].header
- header.add_paragraph('Test Header')
- footer = document.sections[0].footer
- footer.add_paragraph('Test Footer')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement