Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying a simple copy and paste to a new template but getting error #572

Open
Guyfromscarborough opened this issue Nov 12, 2024 · 1 comment

Comments

@Guyfromscarborough
Copy link

Describe your problem

After then file is created, I get an error message when I open the new file
What can I do to avoid having this error show up
Thanks and sorry

Screenshots

error

print("Template document updated to match source document.")

from docx import Document
from docx.oxml import parse_xml
from docx.oxml.ns import nsmap
from copy import deepcopy

# Load the source document
source_doc = Document(r'C:\Users\Tal15\Desktop\Word automate\Source folder\file1_source.docx')

# Create a new Word document
new_doc = Document()

# Function to clear the body of the document
def clear_body(source_doc):
    body = source_doc.element.body
    for child in list(body):
        body.remove(child)

# Clear the body of the template document
clear_body(new_doc)

# Helper function to copy XML content
def copy_element(element):
    return deepcopy(element)

# Copy all elements from source document to template document
for element in source_doc.element.body:
    new_element = copy_element(element)
    new_doc.element.body.append(new_element)

# Save the new document to a specified path
new_doc.save(r'C:\Users\Tal15\Desktop\Word automate\Destination folder\new_file.docx')


print("Template document updated to match source document.")
@elapouya
Copy link
Owner

Are you sure you are on the right repository ? Here it is docxtpl package not docx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants