Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. from docx import Document
  2.  
  3. document = Document()
  4.  
  5. # Add header and footer on all pages
  6.  
  7. document.save("demo.docx")
  8.  
  9. from docx import Document
  10. import win32com.client as win32
  11. import os.path
  12. import tempfile
  13.  
  14. tempdir = tempfile.gettempdir()
  15. msword = win32.gencache.EnsureDispatch('Word.Application')
  16. tempfile = os.path.join(tempdir, "temp.doc")
  17.  
  18. document = Document()
  19.  
  20. document.save(tempfile)
  21.  
  22. doc = msword.Documents.Open(tempfile)
  23.  
  24. doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
  25. doc.Sections(1).Footers(1).PageNumbers.Add()
  26. doc.SaveAs(tempfile, FileFormat = 0)
  27.  
  28. document = Document(tempfile)
  29.  
  30. from docx import Document
  31.  
  32. document = Document()
  33.  
  34. document.add_heading('Document Title', 0)
  35.  
  36. p = document.add_paragraph('A plain paragraph having some ')
  37. p.add_run('bold').bold = True
  38. p.add_run(' and some ')
  39. p.add_run('italic.').italic = True
  40.  
  41. document.add_heading('Heading, level 1', level=1)
  42. document.add_paragraph('Intense quote', style='IntenseQuote')
  43.  
  44. document.add_paragraph(
  45. 'first item in unordered list', style='ListBullet'
  46. )
  47. document.add_paragraph(
  48. 'first item in ordered list', style='ListNumber'
  49. )
  50.  
  51. document.save('demo.docx')
  52.  
  53. from docx import Document
  54.  
  55. document = Document('demo.docx')
  56.  
  57. document.add_heading('A New Title for my Document', 0)
  58.  
  59. p = document.add_paragraph('A new paragraph having some plain ')
  60. p.add_run('bold').bold = True
  61. p.add_run(' and some ')
  62. p.add_run('italic.').italic = True
  63.  
  64. document.add_heading('New Heading, level 1', level=1)
  65. document.add_paragraph('Intense quote', style='IntenseQuote')
  66.  
  67. document.add_paragraph(
  68. 'first new item in unordered list', style='ListBullet'
  69. )
  70. document.add_paragraph(
  71. 'first new item in ordered list', style='ListNumber'
  72. )
  73.  
  74. document.save('demo.docx')
  75.  
  76. from docx import Document
  77.  
  78. document = Document('demo.docx')
  79.  
  80. document.add_page_break()
  81.  
  82. recordset = [ [1, "101", "Spam"], [2, "42", "Eggs"], [3, "631", "Spam, spam, eggs, and spam"]]
  83.  
  84. table = document.add_table(rows=1, cols=3)
  85. hdr_cells = table.rows[0].cells
  86. hdr_cells[0].text = 'Qty'
  87. hdr_cells[1].text = 'Id'
  88. hdr_cells[2].text = 'Desc'
  89.  
  90. for item in recordset:
  91. row_cells = table.add_row().cells
  92. row_cells[0].text = str(item[0])
  93. row_cells[1].text = str(item[1])
  94. row_cells[2].text = item[2]
  95.  
  96. table.style = 'ColorfulShading'
  97.  
  98. document.save('demo.docx')
  99.  
  100. import shutil
  101. shutil.copyfile('demo.docx', 'demo_copy.docx')
  102.  
  103. import win32com.client as win32
  104. msword = win32.gencache.EnsureDispatch('Word.Application')
  105. doc = msword.Documents.Add
  106. doc.Sections(1).Footers(1).Range.Text = r'Text to be included'
  107. doc.Sections(1).Footers(1).PageNumbers.Add()
  108.  
  109. from docx import Document
  110. document = Document("template.docx")
  111. # Do your editing
  112. document.save("demo.docx")
  113.  
  114. unzip template.docx
  115.  
  116. import zipfile
  117.  
  118. document = zipfile.ZipFile("template.docx")
  119. for xml in document.filelist:
  120. if "header" in xml.filename:
  121. read = document.read(xml.filename)
  122. print(read.decode())
  123.  
  124. <w:r><w:t>ThisIsMyHeader</w:t></w:r>
  125.  
  126. from docx import Document
  127. document = Document()
  128.  
  129. header = document.sections[0].header
  130. header.add_paragraph('Test Header')
  131.  
  132. footer = document.sections[0].footer
  133. footer.add_paragraph('Test Footer')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement