By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

converting file formats to txt

P: n/a
Hi All,

Am looking for a python script that can convert fileformats to txt
format. Am unable to find anything in python. Currently the InfoCon
projects looks pretty good to use, but it is return in java.

Thanks and Regards,
Gaurav Agarwal

Jul 4 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In <11*********************@m73g2000cwd.googlegroups. com>, Gaurav Agarwal
wrote:
Am looking for a python script that can convert fileformats to txt
format. Am unable to find anything in python. Currently the InfoCon
projects looks pretty good to use, but it is return in java.
What do you mean by 'fileformats'? A script that automagically converts
*anything* to text? What about pictures?

Ciao,
Marc 'BlackJack' Rintsch
Jul 4 '06 #2

P: n/a
Marc 'BlackJack' Rintsch wrote:
What do you mean by 'fileformats'? A script that automagically converts
*anything* to text? What about pictures?
that's a one-and-a-half-liner:

import Image, sys; print list(Image.open(sys.argv[1]).getdata())

</F>

Jul 4 '06 #3

P: n/a
Hi,

I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
etc) to text format.

Regards,
Gaurav Agarwal

Fredrik Lundh wrote:
Marc 'BlackJack' Rintsch wrote:
What do you mean by 'fileformats'? A script that automagically converts
*anything* to text? What about pictures?

that's a one-and-a-half-liner:

import Image, sys; print list(Image.open(sys.argv[1]).getdata())

</F>
Jul 4 '06 #4

P: n/a
On 2006-07-04, Gaurav Agarwal <ga****************@gmail.comwrote:
I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
etc) to text format.
And I want to win the lottery.

Without having to buy tickets.

--
Grant Edwards grante Yow! You can't hurt
at me!! I have an ASSUMABLE
visi.com MORTGAGE!!
Jul 4 '06 #5

P: n/a
On Tue, 04 Jul 2006 06:32:13 -0700, Gaurav Agarwal wrote:
Hi,

I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
etc) to text format.
RTF, HTML and PS are already text format.

DOC is a secret, closed proprietary format. It will be a lot of work
reverse-engineering it. Perhaps you should consider using existing tools
that already do it -- see, for example, the word processors Abiword and
OpenOffice. They are open-source, so you can read and learn from their
code. Alternatively, you could try some of the suggestions here:

http://www.linux.com/article.pl?sid=06/02/22/201247

Or you could just run through the .doc file, filtering out binary
characters, and display just the text characters. That's a quick-and-dirty
strategy that might help.

PDF is (I believe) a compressed, binary format of PS. Perhaps you should
look at the program pdf2ps -- maybe it will help.

If you explain your needs in a little more detail, perhaps people can give
you answers which are a little more helpful.

--
Steven.

Jul 4 '06 #6

P: n/a
Thanks Steven, Actually i wanted a do text processing for my office
where I can view all files in the system and use the first three to
give a summary of the document. Instead of having somebody actually
entering the summary. Seems there is no one code that can act as
convertor across formats, i'll have to check out convertors for
individual formats.

Thanks and Regards,
Gaurav Agarwal

Steven D'Aprano wrote:
On Tue, 04 Jul 2006 06:32:13 -0700, Gaurav Agarwal wrote:
Hi,

I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
etc) to text format.

RTF, HTML and PS are already text format.

DOC is a secret, closed proprietary format. It will be a lot of work
reverse-engineering it. Perhaps you should consider using existing tools
that already do it -- see, for example, the word processors Abiword and
OpenOffice. They are open-source, so you can read and learn from their
code. Alternatively, you could try some of the suggestions here:

http://www.linux.com/article.pl?sid=06/02/22/201247

Or you could just run through the .doc file, filtering out binary
characters, and display just the text characters. That's a quick-and-dirty
strategy that might help.

PDF is (I believe) a compressed, binary format of PS. Perhaps you should
look at the program pdf2ps -- maybe it will help.

If you explain your needs in a little more detail, perhaps people can give
you answers which are a little more helpful.

--
Steven.
Jul 4 '06 #7

P: n/a
Steven D'Aprano wrote:
On Tue, 04 Jul 2006 06:32:13 -0700, Gaurav Agarwal wrote:
Hi,

I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
etc) to text format.

PDF is (I believe) a compressed, binary format of PS. Perhaps you should
look at the program pdf2ps -- maybe it will help.
--
Steven.
Or try the program pdftotext?
-- Juho Schultz

Jul 4 '06 #8

P: n/a
I suspect you will have to process those formats separately. But the
good news, at least for doc files, is that there is a script in the
Python Cookbook 2Ed that does what you want for MS Word docs and
another script that does it for Open Office docs.

The scripts are 2.26 and 2.27 pages 101-102.

I think you can probably find them at the ActiveState repository also.

http://aspn.activestate.com/ASPN/Coo.../Recipe/279003

In the book, the title of the script is "Extracting Text from Microsoft
Word Documents"

It uses PyWin32 extension and COM to perform the conversion.

rd

Jul 5 '06 #9

P: n/a
Hi All,

Thanks for the advise. Am trying to play around with InfoCon, part of
from Dspace project. It does file conversions. But it is written in
java and uses open office plugin.

Regards,
Gaurav Agarwal

BartlebyScrivener wrote:
I suspect you will have to process those formats separately. But the
good news, at least for doc files, is that there is a script in the
Python Cookbook 2Ed that does what you want for MS Word docs and
another script that does it for Open Office docs.

The scripts are 2.26 and 2.27 pages 101-102.

I think you can probably find them at the ActiveState repository also.

http://aspn.activestate.com/ASPN/Coo.../Recipe/279003

In the book, the title of the script is "Extracting Text from Microsoft
Word Documents"

It uses PyWin32 extension and COM to perform the conversion.

rd
Jul 5 '06 #10

P: n/a
On 4 Jul 2006 08:38:47 -0700, Gaurav Agarwal
<ga****************@gmail.comwrote:
Thanks Steven, Actually i wanted a do text processing for my office
where I can view all files in the system and use the first three to
give a summary of the document. Instead of having somebody actually
entering the summary. Seems there is no one code that can act as
convertor across formats, i'll have to check out convertors for
individual formats.
I have some old code that does just that. It uses pdftotext, catdoc
and links to convert .doc, .pdf and .html to text.

################################################## ################
import mimetypes
from subprocess import call, Popen, PIPE
import sys

class ConversionError(Exception):
pass

class UnknownMimeType(ConversionError):
pass

class NotAMimeType(ConversionError):
pass

class ParseError(ConversionError):
pass

def has_program(progname):
return call(["which", progname], stdout = PIPE) == 0

def check_requirements():
missing = []
for prog in "catdoc", "pdftotext", "links":
if not has_program(prog):
missing.append(prog)
if missing:
print "You need to have the programs:", " ".join(missing)
return False
return True

if not check_requirements():
print "Needed external programs not found, quitting"
sys.exit(1)

def get_catdoc_args(infile):
return ["catdoc", "-s", "8859-1", infile]

def get_pdftotext_args(infile):
return ["pdftotext", infile, "-"]

def get_links_args(infile):
return ["links", infile, "-dump"]

def totext(document):
filetype_to_args_map = {"application/msword" : get_catdoc_args,
"application/pdf" : get_pdftotext_args,
"text/html" : get_links_args}

ftype, ign = mimetypes.guess_type(document)
if not ftype:
raise NotAMimeType, "Couldn't detect mimetype for %s" % document
try:
argfunc = filetype_to_args_map[ftype]
except KeyError:
s = "Don't know how to handle %s documents" % ftype
raise UnknownMimeType, s

p = Popen(argfunc(document), stdout = PIPE, stderr = PIPE)
text = p.stdout.read()
if p.wait():
# Force a better exception to be thrown if the file doesn't exist.
open(document)
raise ParseError, "Failed to parse %s" % document
return text

if __name__ == "__main__":
print totext("testpdf.pdf")

--
mvh Björn
Jul 6 '06 #11

P: n/a
tks this ws really helpful, i used catdoc, catppt, xls2csv, pdftotext
from xdf and ps2txt from ghostview!..

BJörn Lindqvist wrote:
On 4 Jul 2006 08:38:47 -0700, Gaurav Agarwal
<ga****************@gmail.comwrote:
Thanks Steven, Actually i wanted a do text processing for my office
where I can view all files in the system and use the first three to
give a summary of the document. Instead of having somebody actually
entering the summary. Seems there is no one code that can act as
convertor across formats, i'll have to check out convertors for
individual formats.

I have some old code that does just that. It uses pdftotext, catdoc
and links to convert .doc, .pdf and .html to text.

################################################## ################
import mimetypes
from subprocess import call, Popen, PIPE
import sys

class ConversionError(Exception):
pass

class UnknownMimeType(ConversionError):
pass

class NotAMimeType(ConversionError):
pass

class ParseError(ConversionError):
pass

def has_program(progname):
return call(["which", progname], stdout = PIPE) == 0

def check_requirements():
missing = []
for prog in "catdoc", "pdftotext", "links":
if not has_program(prog):
missing.append(prog)
if missing:
print "You need to have the programs:", " ".join(missing)
return False
return True

if not check_requirements():
print "Needed external programs not found, quitting"
sys.exit(1)

def get_catdoc_args(infile):
return ["catdoc", "-s", "8859-1", infile]

def get_pdftotext_args(infile):
return ["pdftotext", infile, "-"]

def get_links_args(infile):
return ["links", infile, "-dump"]

def totext(document):
filetype_to_args_map = {"application/msword" : get_catdoc_args,
"application/pdf" : get_pdftotext_args,
"text/html" : get_links_args}

ftype, ign = mimetypes.guess_type(document)
if not ftype:
raise NotAMimeType, "Couldn't detect mimetype for %s" % document
try:
argfunc = filetype_to_args_map[ftype]
except KeyError:
s = "Don't know how to handle %s documents" % ftype
raise UnknownMimeType, s

p = Popen(argfunc(document), stdout = PIPE, stderr = PIPE)
text = p.stdout.read()
if p.wait():
# Force a better exception to be thrown if the file doesn't exist.
open(document)
raise ParseError, "Failed to parse %s" % document
return text

if __name__ == "__main__":
print totext("testpdf.pdf")

--
mvh Björn
Jul 10 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.