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.
libreoffice conversion
add a comment |
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.
libreoffice conversion
add a comment |
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.
libreoffice conversion
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
libreoffice conversion
asked May 14 '14 at 20:39
Luis Alvarado♦
143k135482649
143k135482649
add a comment |
add a comment |
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. :)
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 usedsubprocess.call
in the command loop, the script waited for each conversion to finish before proceeding. Whensubprocess.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
add a comment |
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
The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
– Nehal J Wani
May 15 '14 at 16:32
add a comment |
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. :)
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 usedsubprocess.call
in the command loop, the script waited for each conversion to finish before proceeding. Whensubprocess.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
add a comment |
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. :)
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 usedsubprocess.call
in the command loop, the script waited for each conversion to finish before proceeding. Whensubprocess.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
add a comment |
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. :)
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. :)
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 usedsubprocess.call
in the command loop, the script waited for each conversion to finish before proceeding. Whensubprocess.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
add a comment |
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 usedsubprocess.call
in the command loop, the script waited for each conversion to finish before proceeding. Whensubprocess.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
add a comment |
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
The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
– Nehal J Wani
May 15 '14 at 16:32
add a comment |
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
The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :)
– Nehal J Wani
May 15 '14 at 16:32
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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