extract text from .doc (not docx) - python

I checked mose question and answers in stackoverflow and others there is many way to open and read the .docx file not doc by using python
I already checked python-docx library but it only support to docx.
I wanna to open and extract text from .doc file(not docx). Plase help me Because I'm new in python

You can use Tika Python, it's an Apache Tika bindings for python. Another good library is a textract.

I created library to extract text from doc files. It works for C and Python
https://github.com/uvoteam/libdoc
usage example:
import extract_doc
with open('./test.doc', 'rb') as myfile:
data = bytearray(myfile.read())
print(extract_doc.extract_doc_text(data, len(data)))

Related

How do I edit repository txt with python

So I am working on a python project for school and I wanted to save some variables to a raw txt. If there is a way to do it by only importing requests that would be great as the program I am using does not import git.
Thanks
-Amir Ahmed
First off, reading/writing from/to files is included in the Python Standard Library. Requests is a HTTPRequests library. The way to write to a text file is like so:
writer = open("path_or_relative_path.txt", "a")
writer.write("testdatatowrite\n")
writer.close()
This is the most basic way of writing to a text file.

Python 3 - Data mining from PDF

I'm working on a project that requires obtaining data from some PDF documents.
Currently I'm using Foxit toolkit (calling it from the script) to convert the document to txt and then I iterate through it. I'm pretty happy with it, but 100$ it's just something I can't afford for such a small project.
I've tested all the free converters that I could find (like xpdf, pdftotext) but they just don't cut it, they mess up the format in a way that i cant use the words to locate the data.
I've tried some Python modules like pdfminer but they don't seem to work well in Python 3.
I can't get the data before it's converted to PDF because I get them from a phone carrier.
I'm looking for a way of getting the data from the PDF or a converter that at least follow the newlines properly.
Update:
PyPDF2 is not grabbing any text whatsoever from the pdf document.
The PyPDF2 seems to be the best one available for Python3
It's well documented and the API is simple to use.
It also can work with encrypted files, retrieve metadata, merge documents, etc
A simple use case for extracting the text:
from PyPDF2 import PdfFileReader
with open("test.pdf",'rb') as f:
if f:
ipdf = PdfFileReader(f)
text = [p.extractText() for p in ipdf.pages]
I don't believe that there is a good free python pdf converter sadly, however pdf2html although it is not a python module, works extremely well and provides you with much more structured data(html) compared to a simple text file. And from there you can use python tools such as beautiful soup to scrape the html file.
link - http://coolwanglu.github.io/pdf2htmlEX/
Hope this helps.
Here is an example of pyPDF2 codes:
from PyPDF2 import PdfFileReader
pdfFileObj = open("FileName", "rb")
pdfReader = PdfFileReader(pdfFileObj,strict = False)
data=[page.extractText() for page in pdfReader.pages]
more information on pyPDF2 here.
I had the same problem when I wanted to do some deep inspection of PDFs for security analysis - I had to write my own utility that parses the low-level objects and literals, unpacks streams, etc so I could get at the "raw data":
https://github.com/opticaliqlusion/pypdf
It's not a feature complete solution, but it is meant to be used in a pure python context where you can define your own visitors to iterate over all the streams, text, id nodes, etc in the PDF tree:
class StreamIterator(PdfTreeVisitor):
'''For deflating (not crossing) the streams'''
def visit_stream(self, node):
print(node.value)
pass
...
StreamIterator().visit(tree)
Anyhow, I dont know if this is the kind of thing you were looking for, but I used it to do some security analysis when looking at suspicious email attachments.
Cheers!

Reading a header from a .docx (Word) file in Python docx

I'm parsing docx files using the library python-docx. I need to read the header of documents as well as paragraphs however I can't find anything about document headers in their documentation. There is documentation on writing a header to a new file but none on reading a header. Is there a way to do this?
I had the same problem.
I used instead of the "python-docx" package a newer version called python-docx2txt
(https://github.com/ankushshah89/python-docx2txt) which extract the text with the headers in one line.

python converter libraries for DOC, DOCX and PDF [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
solution to convert PDFs, DOCs, DOCXs into a textual format with python
I am making a document search engine which indexes popular binary formats. I am looking for python libraries for this purpose.
Reliable converters proved too hard to find. PyPDF never works accurately. Please reccomend:
python libraries that convert these formats to text
or cross-platform, standalone programs that can be called as a subprocess
You can sort of read .docx by unzipping it and then rootling around in the resulting folder structure. See How can I search a word in a Word 2007 .docx file?.
If pyPDF isn't working for you, you can use pdftotext as a subprocess.
.doc is probably the hardest. Is COM scripting an option for you? That is, asking Word to open the file and export it as text? There's a linux utility extracting text from MS word files in python.
You might try Open Office.
It's converting skills are above average. For editing PDF documents, you need to install the pdf import extension.
There are some extensions to work with python, such as the python-uno bridge, but I've had difficulty with it, and generally resort to calling open office as a subprocess.
Just noticed you opened a duplicate question at:
solution to convert PDFs, DOCs, DOCXs into a textual format with python...

Using Python to extract images and text from a word document

I would like to run a script on a folder full of word documents that reads through the documents and pulls out images and their captions (text right below the images). From the research I've done, I think pywin32 might be a viable solution. I know how to use pywin32 to find strings and pull them out, but I need help with the images part. How can I read through a docx file and have an event occur when an image is found? Thank you for any help! I am using Python 2.7.
Docx files can be unzipped for extracting the images.
Find some inspiration in this post How can I search a word in a Word 2007 .docx file?
You can use the python module docx2txt for extracting text as well as images from docx files
document =docx.Document(filepath)
for image in document.inline_shapes:
print (image.width, image.height)
Try this it will work.

Categories