Copying Header of 1 docx file as it is and Pasting it on other docx using Python docx (including font style and logo/images etc)
How to copy a header of one file and paste it on other files using python docx? I have already written some code but it's gives an error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\HP15DA~1\AppData\Local\Temp/ipykernel_7648/3264864237.py in <module>
19 # Set the header of the first section in the new document to the new_header
20
---> 21 new_doc.sections[0].header = new_header
22
23 # Save the new document
AttributeError: can't set attribute`
from docx import Document
# Open the source document
source_doc = Document('1.docx')
# Get the header from the source document
header = source_doc.sections[0].header
# Create a new document
new_doc = Document()
# Copy the content of the header to the new document
new_header = new_doc.sections[0].header
#new_header._element.remove_all()
for child in header._element:
new_header._element.append(child)
#print(new_header)
# Set the header of the first section in the new document to the new_header
new_doc.sections[0].header = new_header
# Save the new document
new_doc.save('newument.docx')
source https://stackoverflow.com/questions/75683725/copying-header-of-1-docx-file-as-it-is-and-pasting-it-on-other-docx-using-python
Comments
Post a Comment