Answer the question
In order to leave comments, you need to log in
How to copy part of one docx document to another from Python?
Hello, I came across a tricky but very interesting problem.
I need to write a python function that is passed 5 values:
source_docx # path - docx файл, из которого нужно забрать данные
outputdocx # path - docx, в который они будут вставлены
trimStart # str - фраза, с которой нужно обрезать данные (к примеру, "глава 1")
trimEnd # str - фраза, по которую данные будут обрезаны (к примеру, "глава 2")
relpace # str - строчка в outputdocx, которую нужно заменить ( к примеру {{replaceme}})
Answer the question
In order to leave comments, you need to log in
If it is from a file, then, most likely, not directly. Just unzip the .DOCX and manually search the appropriate files for content describing the text at the beginning and end of the phrase. by words and tags. But it is not a fact that the copied text can be saved as a valid DOCX, because DOCX has a very complex structure. That is, it will be necessary to create a lot of additional things before packing it into DOCX.
I think it's better to do it with built-in Word tools.
Take something like python-docx and iterate over paragraphs and other blocks. Until you find trimStart - delete everything to hell :) you do not touch the necessary, there is a possibility that the layout will not work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question