converting file formats to txt

G

Gaurav Agarwal

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
 
M

Marc 'BlackJack' Rintsch

Gaurav Agarwal said:
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
 
F

Fredrik Lundh

Marc said:
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>
 
G

Gaurav Agarwal

Hi,

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

Regards,
Gaurav Agarwal

Fredrik said:
Marc said:
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>
 
G

Grant Edwards

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.
 
S

Steven D'Aprano

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.
 
G

Gaurav Agarwal

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
 
J

Juho Schultz

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

Or try the program pdftotext?


-- Juho Schultz
 
D

Dennis Lee Bieber

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.
Well, if you are on Windows (and it might even work on other
systems), you might be able to "install" a printer driver for an old
text-only printer (a driver for a daisy-wheel printer, perhaps)... The
configure said driver to "print to file"... For each file, invoke
whatever application has a command line "print" operation, and read the
resultant print file... <G>

PDF and PostScript are both programming /languages/ optimized for
rendering text layouts. Nothing requires the text in the files to be in
the same order it will appear on the page. Two different applications
can generate massively different PostScript for the same "page",
depending upon the intelligence used in the creating program -- I've
seen one program that included a justification subroutine within the
standard preamble, so justified text lines appear as (pseudocode):

x y move "This is the line of text to be justified" 576 justify

(the 576 is an 8" line in points)

Another program did word justification with stuff like:

x y move "This" put x1 y move "is" put x2 y move "the" ...

where x, x1, x2 have been precomputed based upon the font metrics file.
A third possibility does letter spaced justification, but relies on the
application to control all the spacing:

x y move "T" put x1 y move "h" put ...


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

BartlebyScrivener

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/Cookbook/Python/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
 
G

Gaurav Agarwal

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
 
D

Dennis Lee Bieber

from Dspace project. It does file conversions. But it is written in
java and uses open office plugin.
Sounds like "it" does NOT do file conversions either -- but is using
OpenOffice to perform the conversions behind the scenes.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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")
 
G

Gaurav Agarwal

tks this ws really helpful, i used catdoc, catppt, xls2csv, pdftotext
from xdf and ps2txt from ghostview!..

BJörn Lindqvist said:
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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top