Convert multiple document files inside their current folders











up vote
4
down vote

favorite












I have a folder called X which inside has many folders and sub-folders. I want to search in all of the folders inside X and find a specific document type. For example xlsx and transform each file into an xls file. In this case I need to use the libreoffice convert option like libreoffice --headless --convert-to xls but here is the catch. After conversion each converted file should be in the same folder as the original xlsx file.



Example:



X/
Folder 1/
Folder 2/
Folder 2.1/file.xlsx
Folder 3/
Folder 4/
Folder 4.1/anotherFile.xlsx


After conversion:



X/
Folder 1/
Folder 2/
Folder 2.1/file.xls
Folder 2.1/file.xlsx
Folder 3/
Folder 4/
Folder 4.1/anotherFile.xls
Folder 4.1/anotherFile.xlsx


The new xls* in this case, will be converted on the same folder as the original xlsx. This will be done inside all sub-folders and to all xlsx that are found.










share|improve this question


























    up vote
    4
    down vote

    favorite












    I have a folder called X which inside has many folders and sub-folders. I want to search in all of the folders inside X and find a specific document type. For example xlsx and transform each file into an xls file. In this case I need to use the libreoffice convert option like libreoffice --headless --convert-to xls but here is the catch. After conversion each converted file should be in the same folder as the original xlsx file.



    Example:



    X/
    Folder 1/
    Folder 2/
    Folder 2.1/file.xlsx
    Folder 3/
    Folder 4/
    Folder 4.1/anotherFile.xlsx


    After conversion:



    X/
    Folder 1/
    Folder 2/
    Folder 2.1/file.xls
    Folder 2.1/file.xlsx
    Folder 3/
    Folder 4/
    Folder 4.1/anotherFile.xls
    Folder 4.1/anotherFile.xlsx


    The new xls* in this case, will be converted on the same folder as the original xlsx. This will be done inside all sub-folders and to all xlsx that are found.










    share|improve this question
























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have a folder called X which inside has many folders and sub-folders. I want to search in all of the folders inside X and find a specific document type. For example xlsx and transform each file into an xls file. In this case I need to use the libreoffice convert option like libreoffice --headless --convert-to xls but here is the catch. After conversion each converted file should be in the same folder as the original xlsx file.



      Example:



      X/
      Folder 1/
      Folder 2/
      Folder 2.1/file.xlsx
      Folder 3/
      Folder 4/
      Folder 4.1/anotherFile.xlsx


      After conversion:



      X/
      Folder 1/
      Folder 2/
      Folder 2.1/file.xls
      Folder 2.1/file.xlsx
      Folder 3/
      Folder 4/
      Folder 4.1/anotherFile.xls
      Folder 4.1/anotherFile.xlsx


      The new xls* in this case, will be converted on the same folder as the original xlsx. This will be done inside all sub-folders and to all xlsx that are found.










      share|improve this question













      I have a folder called X which inside has many folders and sub-folders. I want to search in all of the folders inside X and find a specific document type. For example xlsx and transform each file into an xls file. In this case I need to use the libreoffice convert option like libreoffice --headless --convert-to xls but here is the catch. After conversion each converted file should be in the same folder as the original xlsx file.



      Example:



      X/
      Folder 1/
      Folder 2/
      Folder 2.1/file.xlsx
      Folder 3/
      Folder 4/
      Folder 4.1/anotherFile.xlsx


      After conversion:



      X/
      Folder 1/
      Folder 2/
      Folder 2.1/file.xls
      Folder 2.1/file.xlsx
      Folder 3/
      Folder 4/
      Folder 4.1/anotherFile.xls
      Folder 4.1/anotherFile.xlsx


      The new xls* in this case, will be converted on the same folder as the original xlsx. This will be done inside all sub-folders and to all xlsx that are found.







      libreoffice conversion






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 14 '14 at 20:39









      Luis Alvarado

      143k135482649




      143k135482649






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Here is a python script that does as you described.



          For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).



          note: In some cases (like mine), the command



          libreoffice --headless --convert-to xls


          only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:



          sudo chown -R --reference="$HOME" ~/.config


          as described here.



          The script:



          #!/usr/bin/python3

          convert_dir = "/path/to/folder/tobeconverted"
          import os
          import subprocess

          for root, dirs, files in os.walk(convert_dir):
          for name in files:
          if name.endswith(".xlsx"):
          # filepath+name
          file = root+"/"+name
          destination = root
          subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
          else:
          pass


          Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:



          python3 /path/to/scrip/script.py


          But I am pretty sure you know that. :)






          share|improve this answer























          • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
            – Luis Alvarado
            May 15 '14 at 14:37






          • 1




            @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
            – Jacob Vlijm
            May 15 '14 at 16:16












          • Well that was dramatically faster!
            – Luis Alvarado
            May 15 '14 at 16:32


















          up vote
          3
          down vote













          This works for me:



          cd X
          find ./ -iname "*.xlsx" > /tmp/out
          SAVEIFS=$IFS;
          IFS=$(echo -en "nb");
          while read line ;
          do
          cd $(dirname $line);
          libreoffice --headless --convert-to xls $(basename $line);
          echo $PWD;
          cd -;
          done < /tmp/out;
          IFS=$SAVEIFS





          share|improve this answer























          • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
            – Nehal J Wani
            May 15 '14 at 16:32











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f465988%2fconvert-multiple-document-files-inside-their-current-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Here is a python script that does as you described.



          For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).



          note: In some cases (like mine), the command



          libreoffice --headless --convert-to xls


          only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:



          sudo chown -R --reference="$HOME" ~/.config


          as described here.



          The script:



          #!/usr/bin/python3

          convert_dir = "/path/to/folder/tobeconverted"
          import os
          import subprocess

          for root, dirs, files in os.walk(convert_dir):
          for name in files:
          if name.endswith(".xlsx"):
          # filepath+name
          file = root+"/"+name
          destination = root
          subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
          else:
          pass


          Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:



          python3 /path/to/scrip/script.py


          But I am pretty sure you know that. :)






          share|improve this answer























          • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
            – Luis Alvarado
            May 15 '14 at 14:37






          • 1




            @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
            – Jacob Vlijm
            May 15 '14 at 16:16












          • Well that was dramatically faster!
            – Luis Alvarado
            May 15 '14 at 16:32















          up vote
          3
          down vote



          accepted










          Here is a python script that does as you described.



          For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).



          note: In some cases (like mine), the command



          libreoffice --headless --convert-to xls


          only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:



          sudo chown -R --reference="$HOME" ~/.config


          as described here.



          The script:



          #!/usr/bin/python3

          convert_dir = "/path/to/folder/tobeconverted"
          import os
          import subprocess

          for root, dirs, files in os.walk(convert_dir):
          for name in files:
          if name.endswith(".xlsx"):
          # filepath+name
          file = root+"/"+name
          destination = root
          subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
          else:
          pass


          Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:



          python3 /path/to/scrip/script.py


          But I am pretty sure you know that. :)






          share|improve this answer























          • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
            – Luis Alvarado
            May 15 '14 at 14:37






          • 1




            @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
            – Jacob Vlijm
            May 15 '14 at 16:16












          • Well that was dramatically faster!
            – Luis Alvarado
            May 15 '14 at 16:32













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Here is a python script that does as you described.



          For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).



          note: In some cases (like mine), the command



          libreoffice --headless --convert-to xls


          only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:



          sudo chown -R --reference="$HOME" ~/.config


          as described here.



          The script:



          #!/usr/bin/python3

          convert_dir = "/path/to/folder/tobeconverted"
          import os
          import subprocess

          for root, dirs, files in os.walk(convert_dir):
          for name in files:
          if name.endswith(".xlsx"):
          # filepath+name
          file = root+"/"+name
          destination = root
          subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
          else:
          pass


          Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:



          python3 /path/to/scrip/script.py


          But I am pretty sure you know that. :)






          share|improve this answer














          Here is a python script that does as you described.



          For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).



          note: In some cases (like mine), the command



          libreoffice --headless --convert-to xls


          only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:



          sudo chown -R --reference="$HOME" ~/.config


          as described here.



          The script:



          #!/usr/bin/python3

          convert_dir = "/path/to/folder/tobeconverted"
          import os
          import subprocess

          for root, dirs, files in os.walk(convert_dir):
          for name in files:
          if name.endswith(".xlsx"):
          # filepath+name
          file = root+"/"+name
          destination = root
          subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
          else:
          pass


          Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:



          python3 /path/to/scrip/script.py


          But I am pretty sure you know that. :)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 25 '17 at 17:58

























          answered May 15 '14 at 7:03









          Jacob Vlijm

          62.8k9120215




          62.8k9120215












          • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
            – Luis Alvarado
            May 15 '14 at 14:37






          • 1




            @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
            – Jacob Vlijm
            May 15 '14 at 16:16












          • Well that was dramatically faster!
            – Luis Alvarado
            May 15 '14 at 16:32


















          • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
            – Luis Alvarado
            May 15 '14 at 14:37






          • 1




            @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
            – Jacob Vlijm
            May 15 '14 at 16:16












          • Well that was dramatically faster!
            – Luis Alvarado
            May 15 '14 at 16:32
















          Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
          – Luis Alvarado
          May 15 '14 at 14:37




          Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if?
          – Luis Alvarado
          May 15 '14 at 14:37




          1




          1




          @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
          – Jacob Vlijm
          May 15 '14 at 16:16






          @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster.
          – Jacob Vlijm
          May 15 '14 at 16:16














          Well that was dramatically faster!
          – Luis Alvarado
          May 15 '14 at 16:32




          Well that was dramatically faster!
          – Luis Alvarado
          May 15 '14 at 16:32












          up vote
          3
          down vote













          This works for me:



          cd X
          find ./ -iname "*.xlsx" > /tmp/out
          SAVEIFS=$IFS;
          IFS=$(echo -en "nb");
          while read line ;
          do
          cd $(dirname $line);
          libreoffice --headless --convert-to xls $(basename $line);
          echo $PWD;
          cd -;
          done < /tmp/out;
          IFS=$SAVEIFS





          share|improve this answer























          • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
            – Nehal J Wani
            May 15 '14 at 16:32















          up vote
          3
          down vote













          This works for me:



          cd X
          find ./ -iname "*.xlsx" > /tmp/out
          SAVEIFS=$IFS;
          IFS=$(echo -en "nb");
          while read line ;
          do
          cd $(dirname $line);
          libreoffice --headless --convert-to xls $(basename $line);
          echo $PWD;
          cd -;
          done < /tmp/out;
          IFS=$SAVEIFS





          share|improve this answer























          • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
            – Nehal J Wani
            May 15 '14 at 16:32













          up vote
          3
          down vote










          up vote
          3
          down vote









          This works for me:



          cd X
          find ./ -iname "*.xlsx" > /tmp/out
          SAVEIFS=$IFS;
          IFS=$(echo -en "nb");
          while read line ;
          do
          cd $(dirname $line);
          libreoffice --headless --convert-to xls $(basename $line);
          echo $PWD;
          cd -;
          done < /tmp/out;
          IFS=$SAVEIFS





          share|improve this answer














          This works for me:



          cd X
          find ./ -iname "*.xlsx" > /tmp/out
          SAVEIFS=$IFS;
          IFS=$(echo -en "nb");
          while read line ;
          do
          cd $(dirname $line);
          libreoffice --headless --convert-to xls $(basename $line);
          echo $PWD;
          cd -;
          done < /tmp/out;
          IFS=$SAVEIFS






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 at 9:44









          David Foerster

          27.2k1363107




          27.2k1363107










          answered May 14 '14 at 20:58









          Nehal J Wani

          443313




          443313












          • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
            – Nehal J Wani
            May 15 '14 at 16:32


















          • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
            – Nehal J Wani
            May 15 '14 at 16:32
















          The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
          – Nehal J Wani
          May 15 '14 at 16:32




          The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
          – Nehal J Wani
          May 15 '14 at 16:32


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f465988%2fconvert-multiple-document-files-inside-their-current-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei