472,334 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

converting file formats to txt

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
11 4318
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sturnoff Megantic | last post by:
I would like to allow my users to upload a large wav file and have it converted to one of the following: a smaller wav file, an MP3, RA or WMA. ...
3
by: Moritz Tacke | last post by:
Hi there! I have a problem concerning the file input to a c++ program: the expected input is either in binary or in some kind of text format;...
5
by: Rishit | last post by:
Hello All, I have to convert XML data (that may be received through a dataset) to other formats such as CSV, Excel etc. Are there any libraries or...
2
by: Subodh | last post by:
Hi, Currently we get data from more then 200 different sources and all of our vendors provide data in different file formats. The problem is we...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By...
8
by: spacepie | last post by:
Does anyone have sample coding to do this. I have a legacy app which generates great data but it's in PRN format. I need to convert this to CSV...
1
by: Rishit | last post by:
Hello All, I have to convert XML data (that may be received through a dataset) to other formats such as CSV, Excel etc. Are there any libraries or...
8
by: Michael B. Trausch | last post by:
I was wondering if anyone has had any experience with this. Someone I know is trying to move away from Microsoft Works, and I am trying to look...
5
by: yawnmoth | last post by:
XSL stylesheets can be used to convert an XML file into whatever binary format you want (DocBook, for example, does PDF's). My question is... ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.