converting file formats to txt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gaurav Agarwal

    #1

    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

  • Marc 'BlackJack' Rintsch

    #2
    Re: converting file formats to txt

    In <1151995347.533 312.99390@m73g2 000cwd.googlegr oups.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

    Comment

    • Fredrik Lundh

      #3
      Re: converting file formats to txt

      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>



      Comment

      • Gaurav Agarwal

        #4
        Re: converting file formats to txt

        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>

        Comment

        • Grant Edwards

          #5
          Re: converting file formats to txt

          On 2006-07-04, Gaurav Agarwal <gaurav.agarwal 1904@gmail.comw rote:
          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!!

          Comment

          • Steven D'Aprano

            #6
            Re: converting file formats to txt

            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:



            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.

            Comment

            • Gaurav Agarwal

              #7
              Re: converting file formats to txt

              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:
              >

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

              Comment

              • Juho Schultz

                #8
                Re: converting file formats to txt

                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

                Comment

                • BartlebyScrivener

                  #9
                  Re: converting file formats to txt

                  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.



                  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

                  Comment

                  • Gaurav Agarwal

                    #10
                    Re: converting file formats to txt

                    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

                    BartlebyScriven er 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.
                    >

                    >
                    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

                    Comment

                    • BJörn Lindqvist

                      #11
                      Re: converting file formats to txt

                      On 4 Jul 2006 08:38:47 -0700, Gaurav Agarwal
                      <gaurav.agarwal 1904@gmail.comw rote:
                      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 (ConversionErro r):
                      pass

                      class NotAMimeType(Co nversionError):
                      pass

                      class ParseError(Conv ersionError):
                      pass

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

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

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

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

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

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

                      def totext(document ):
                      filetype_to_arg s_map = {"applicatio n/msword" : get_catdoc_args ,
                      "applicatio n/pdf" : get_pdftotext_a rgs,
                      "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_arg s_map[ftype]
                      except KeyError:
                      s = "Don't know how to handle %s documents" % ftype
                      raise UnknownMimeType , s

                      p = Popen(argfunc(d ocument), 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

                      Comment

                      • Gaurav Agarwal

                        #12
                        Re: converting file formats to txt

                        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
                        <gaurav.agarwal 1904@gmail.comw rote:
                        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 (ConversionErro r):
                        pass
                        >
                        class NotAMimeType(Co nversionError):
                        pass
                        >
                        class ParseError(Conv ersionError):
                        pass
                        >
                        def has_program(pro gname):
                        return call(["which", progname], stdout = PIPE) == 0
                        >
                        def check_requireme nts():
                        missing = []
                        for prog in "catdoc", "pdftotext" , "links":
                        if not has_program(pro g):
                        missing.append( prog)
                        if missing:
                        print "You need to have the programs:", " ".join(miss ing)
                        return False
                        return True
                        >
                        if not check_requireme nts():
                        print "Needed external programs not found, quitting"
                        sys.exit(1)
                        >
                        def get_catdoc_args (infile):
                        return ["catdoc", "-s", "8859-1", infile]
                        >
                        def get_pdftotext_a rgs(infile):
                        return ["pdftotext" , infile, "-"]
                        >
                        def get_links_args( infile):
                        return ["links", infile, "-dump"]
                        >
                        def totext(document ):
                        filetype_to_arg s_map = {"applicatio n/msword" : get_catdoc_args ,
                        "applicatio n/pdf" : get_pdftotext_a rgs,
                        "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_arg s_map[ftype]
                        except KeyError:
                        s = "Don't know how to handle %s documents" % ftype
                        raise UnknownMimeType , s
                        >
                        p = Popen(argfunc(d ocument), 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

                        Comment

                        Working...