I want to delete text of multiple docx file using python language.
Let's say the contents of a file are:
This is line 1
This is line 2
This is line 3
This is line 4
I want to delete the very last line only i.e. This is line 4
.
I've tried many code but getting errors.
Try 1:
with open(r"FILE_PATH.docx", 'r+', errors='ignore') as fp:
# read an store all lines into list
lines = fp.readlines()
# move file pointer to the beginning of a file
fp.seek(0)
# truncate the file
fp.truncate()
# start writing lines except the last line
# lines[:-1] from line 0 to the second last line
fp.writelines(lines[:-1])
Above code runs with 0 errors but getting some loss of data in the docx file.