copying an Excelsheet with its conent and renaming into different names - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: copying an Excelsheet with its conent and renaming into different names (/thread-27344.html) |
copying an Excelsheet with its conent and renaming into different names - deheugden - Jun-03-2020 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. RE: copying an Excelsheet with its conent and renaming into different names - Gribouillis - Jun-03-2020 The shutil module can copy files, if this is the sense of your question import shutil shutil.copy2('spam.xls', 'eggs.xls') RE: copying an Excelsheet with its conent and renaming into different names - deheugden - Jun-04-2020 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. RE: copying an Excelsheet with its conent and renaming into different names - Gribouillis - Jun-04-2020 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"]) RE: copying an Excelsheet with its conent and renaming into different names - deheugden - Jun-04-2020 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. RE: copying an Excelsheet with its conent and renaming into different names - Gribouillis - Jun-04-2020 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. RE: copying an Excelsheet with its conent and renaming into different names - deheugden - Jun-05-2020 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/6351186/reading-a-csv-files-columns-directly-into-variables-names-with-python But that is still abracadabra for me. But i am still looking for a faster way to import the predefined names. |