0

I used OCR to extract text from 200 images now I have 200 .txt files that I must copy all .txt file into single word document.I search for two solutions

immediate: find a way to copy all text to a .doc file automatically.

ideal: first copy image and then its OCR text automatically. for example copy 29.jpg to word document and then copy 29.txt and so on... any suggestion is welcomed.

OS: Windows 10

Python: installed

Text: UTF-8

1
1

Create a batch file with the following:

CD OCRFolder
copy OCR*.txt > NewFile.txt
C:\Program Files\Microsoft Office\Office15\winword.exe Newfile.txt
del OCR.txt

This will navigate to the folder that holds the OCR files. - CD OCRFolder
Merge all the OCR text files into 1 text file - copy OCR*.txt > NewFile.txt
Open the new file in Word - C:\Program Files\Microsoft Office\Office15\winword.exe Newfile.txt

Your location of WinWOrd.exe may be different depending on your version of Word.

2
  • winword.exe should be in your path, so you can just write winword.exe. – Ploni Jun 2 '17 at 15:41
  • I find that "Should Be" and "Are" are usually two different things. That's why I explicitly added the path. ;-) – B540Glenn Jun 2 '17 at 19:16
1

Since you're asking to combine them into a .doc file, I'm assuming you have Microsoft Office installed.

Open Microsoft Office, selecting the Insert tab. Click the down arrow next to Object, and select the Text from File option.

Press CtrlA to select all the files you want to join together.

Note: if any conversion is necessary, it will mean a few extra clicks... per file!

To add the pictures to the document, on the same Insert tab there's an option to insert pictures, here you can press CtrlA to add all your pictures. However, it will not be able add them alternating after your text.

3
  • thank you, it works just costs 200 clicks :). how about the second? – Iman Jun 2 '17 at 20:50
  • @imi If they're the only files in the folder you can just do ctrl-a (and if not, but they're consecutive, you can use shift and page-down, etc.). As far as the second request, order them alternatively, e.g. 1.txt 1.jpg 2.txt etc., and it should (I haven't tried it) combine them in that order. – Ploni Jun 2 '17 at 20:57
  • I mean 200 clicks for confirmation of utf-8 encoding and ... not selecting files. for the second solution when I insert jpg file as text word converts it to text (so messy). your first solution is ok :) – Iman Jun 2 '17 at 21:30
0

all suggestions work for the first request. thank you

I used the link MSC provided and write a script in python which is a solution for the second request. however, because of utf-8 encoding problem in Windows machine (made me crazy), it just works on a Linux if you know how to overcome utf-8 encoding problem in windows please comment.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from docx import Document
from docx.shared import Inches
document = Document()
p = document.add_paragraph()
r = p.add_run()
for i in range(29,48):
    r.add_picture(str(i)+'.jpg',width=Inches(6.2), height=Inches(9))
    with open(str(i)+'.txt','r') as f:
        for line in f:
            r.add_text(line)
document.save('test.docx')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.