Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Bash: How to add text to a .MS Word (doc) file or an .html file?

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...