Python Forum

Full Version: copying an Excelsheet with its conent and renaming into different names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have been playing with Python for several months now, but progress is going very slow. I am still doing small scripts, am stuck at the functions part, still trying to figure that out.

Anyway, but that is not my question.
I have got an Excelsheet(with content) that needs to be copied with several predefined names.I got no clue how to start that.
Anyone who knows if there are default scripts available that can do the trick?

Thanks in advance.
The shutil module can copy files, if this is the sense of your question
import shutil

shutil.copy2('spam.xls', 'eggs.xls')
many thanks for the reply.
I have been playing with it;
# Importing the modules
import os
import shutil

src_dir = os.getcwd() #get the current working dir


shutil.copy("c:/copy excel/a.xlsm", "c:/copy excel/B.xlsm")
shutil.copy("c:/copy excel/a.xlsm", "c:/copy excel/c.xlsm")
shutil.copy("c:/copy excel/a.xlsm", "c:/copy excel/d.xlsm")

But i dont think this is logical. I think it should be possible, somehow, to add a list with names and that all the new files get one of those names.
Well, you can use a loop to make several copies
import shutil
from pathlib import Path

def multicopy2(src, idst):
    for dst in idst:
        shutil.copy2(src, dst)

p = Path("C:")/"copy excel"
multicopy2(p/"a.xlsm", [p/"b.xlsm", p/"c.xlsm", p/"d.xlsm"])
Many thanks for the reply.
I created a folder at my c, called copy excel
copied the main sheet and the script into this sheet, but nothings happens when running it.
do i need to add anything?

I also played with the working directory, hoped it is possible to run a script without knowing is actual path.
deheugden Wrote:nothings happens when running it.
It is very unlikely that nothing happens. Is there no error message? Try to add some print statements in the script to see what python does and which paths are used.
am getting the following error:
Traceback (most recent call last):
File "C:\copyexcel\copy excelv3.py", line 2, in <module>
from pathlib import Path
ImportError: No module named pathlib

And after investigating i noticed the idle i use(integrated into explorer_ is an older Python, 2.7. although i have 3.x installed. So i got another challenge :-)

Anyway, isnt it possible to put predefined names in a variable that can be used in the loop to create the copied files with those names? I was also watching this
https://stackoverflow.com/questions/6351...ith-python
But that is still abracadabra for me.

But i am still looking for a faster way to import the predefined names.