473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4586
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. My users have access to only large files, and it...
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; what I finally want is the binary version. I know...
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 sample code that would help me do this task. I...
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 have more then 100 DTS packages now and the...
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 "common" I mean at the least ones described in this...
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 format for importing. I don't know C++ well enouch...
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 sample code that would help me do this task. I...
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 into a solution that would convert their data in a...
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... what if you wanted to go in the other direction? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.