Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am a beginner in bash and have been trying to solve one annoying problem - I cannot add text to a MS Word (.doc) file.

I managed to extract text but not insert one. I tried using sed command but it ruins the file - I suppose because it adds the text to the file's 'source code' (not sure if that is the correct term). I also tried adding text to an .html file (using the same commands) and it still doesn't work. It only works with a simple .txt file.

Commands I have tried:

$: sed -i 'a/existingTest/newText' MyFile.doc

OR

$: sed "/existingText/a newText" MyFile.doc
# I use "existingTest" to identify the location where I want to append my newText.

With both commands the text is added (whether it is applied on a .doc or .html file) inside the 'source code' which makes the file unreadable.

Does anyone knows a way to add text to a .doc or an .html file? Preferably a soulution that a beginner will understand but I will welcome anything :D

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

If your MS Word file has *.docx extention, would you please try:

unzip MyFile.docx
sed -i 'a/existingTest/newText' word/document.xml
zip -f MyFile.docx

As Bing Wang comments, docx document is a zipped file. Then once unzip the file, edit, then update the zip file reflecting the edit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...