Renaming multiple files on a folder python of a excel spreadsheet

感情迁移 提交于 2019-12-12 00:33:51

问题


I am pretty new at Python and I want to automate a process that takes a lot of my time but now. I need to rename about 200 + excel files that need to be renamed, but here is the catch they cant be consequential instead they need to be "vlooked up" of a master spreadsheet that contains the names and then name the files exactly how its named on the master spreadsheet. So for example:

folder I got:

 1. filexxxxFA9261
 2. filexxxxFA3040
 3. filexxxxFA5251

and on my master spreadsheet I got:

 1. FA9261.4586.56641.1212
 2. FA3040.6589.65555.1516
 3. FA5251.3562.65555.5633

basically I just need to vlookup the "FA###" string of the master spreadsheet and rename that file according to what says on the master spreadsheet.

Can someone point me in the right direction? I have been trying to look it up everywhere but most renaming is done sequentially and not of a spreadsheet individually.

Here is an exact example on how it should look like:

This is how the file it’s named initially (it varies depending on who sent it):

“ASSET MOVEMENT 215-156 6C

And this is how it needs to look like exactly

146543115.NC251.LM5555989565C2-.215-156.NSD6C.556562443.MRO232324564612


回答1:


Something like this may help using pandas to deal with the Excel spreadsheet. I'm assuming 6C is relevant from the master spreadsheet.

import pandas
import shutil

master = pandas.io.excel.read_excel('master.xlsc', 'sheet1')
for r in master:
    key1 = r.split('.')[3]
    key2 = r.split('.')[4][-2:]
    old = 'ASSET MOVEMENT %s %s' % (key1, key2)
    new = r
    shutil.move(old, new)


来源:https://stackoverflow.com/questions/30223601/renaming-multiple-files-on-a-folder-python-of-a-excel-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!