I need to create an api where user will provide indesign file and I need to extract text from it.
Asked
Active
Viewed 948 times
-1
-
Huh? Read [ask] and create a [mcve] – Alon Eitan Mar 17 '21 at 14:10
-
@AlonEitan I have indesign files (.indd) and I need to extract all the text from it,is there any way to do so, this is all i'm asking – jyoti sharma Mar 17 '21 at 17:08
-
Do you have InDesign server setup that would be much easier for indesign operations – M Khalid Junaid May 21 '21 at 20:52
1 Answers
0
Basically it can be done this way:
from win32com.client import Dispatch
app = Dispatch('InDesign.Application.CS6')
doc = app.Open(r"d:\text.indd")
contents = ""
for story in doc.stories: contents += story.contents + "\n"
f = open(r"d:\text.txt", "w", encoding="utf8")
f.write(contents)
f.close()
doc.Close()
But perhaps there can be glitches with special symbols. I believe it makes sense to use the native Javascript Extendscript for this task. Something like this:
var doc = app.open(File("d:/text.indd"));
var stories = doc.stories.everyItem().getElements();
var contents = "";
for (var i=0; i<stories.length; i++) contents += stories[i].contents + "\n";
var file = File("d:/text.txt");
file.open("w");
file.write(contents);
file.close();
doc.close();

Yuri Khristich
- 13,448
- 2
- 8
- 23