Python Forum
copying an Excelsheet with its conent and renaming into different names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
copying an Excelsheet with its conent and renaming into different names
#1
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.
Reply
#2
The shutil module can copy files, if this is the sense of your question
import shutil

shutil.copy2('spam.xls', 'eggs.xls')
Reply
#3
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.
Reply
#4
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"])
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 337 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,510 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 1,744 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Python with win32com and EXIF renaming files. Amrcodes 4 3,656 Apr-03-2021, 08:51 PM
Last Post: DeaD_EyE
  Copying a file Kundan 1 2,084 Aug-21-2019, 09:55 AM
Last Post: snippsat
  renaming the file in a suitable format, I just wondering if it is possible go127a 11 5,142 Jun-26-2019, 06:15 AM
Last Post: snippsat
  Renaming explorer files in order? stroudie2 2 2,873 Mar-03-2019, 12:41 AM
Last Post: stroudie2
  script for renaming batch of files rado_789 5 3,308 Nov-07-2018, 02:18 PM
Last Post: rado_789
  Need help with file renaming edlentz 3 2,531 Aug-06-2018, 04:28 PM
Last Post: ichabod801
  Renaming PDF files using Excel data - Python okanaira 4 6,919 Mar-20-2018, 07:31 PM
Last Post: okanaira

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020