Python文件PDF重命名
我在文件夹中有一个.pdf,并且有一个带有两列的.xls。在第一列中,我的文件名没有扩展名.pdf,在第二列中,我有一个值。
我需要打开文件.xls,将第一列中的值与文件夹中的所有文件名匹配,然后将每个文件.pdf重命名为第二列中的值。
是否可以?
谢谢您的支持 安吉洛
I have a file .pdf in a folder and I have a .xls with two-column. In the first column I have the filename without extension .pdf and in the second column, I have a value.
I need to open file .xls, match the value in the first column with all filenames in the folder and rename each file .pdf with the value in the second column.
Is it possible?
Thank you for your support
Angelo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要在Python中使用熊猫库。它具有称为
pandas.read_excel
的函数,对于读取Excel文件非常有用。这将返回数据框,该框架将允许您使用iLoc
或其他在第一列和第二列中访问值的方法。从那里,我建议使用os.Rename(old_name,new_name)
,其中old_name和new_name是保留.pdf文件的路径。重命名部分的一个完整示例看起来像这样:我故意省略了一个完整的解释,因为您只是问是否有可能完成任务,因此希望这将您指向正确的方向!我建议将来根据 Stackoverflow GuideLines 提出问题。
You'll want to use the pandas library within python. It has a function called
pandas.read_excel
that is very useful for reading excel files. This will return a dataframe, which will allow you to useiloc
or other methods of accessing the values in the first and second columns. From there, I'd recommend usingos.rename(old_name, new_name)
, where old_name and new_name are the paths to where your .pdf files are kept. A full example of the renaming part looks like this:I've purposely left out a full explanation because you simply asked if it is possible to achieve your task, so hopefully this points you in the right direction! I'd recommend asking questions with specific reproducible code in the future, in accordance with stackoverflow guidelines.
我鼓励您使用.csv文件而不是XLS进行此操作,这是一种更容易的格式(需要0格式的边框,颜色等格式)。
您可以使用OS.listDir()函数列出某个目录中的所有文件和文件夹。检查OS内置库文档。然后获取每个文件的字符串名称,删除.pdf,然后读取带有名称和值的.CSV文件,以及重命名文件。
所需的所有实用程序都是内置的Python。大多数是OS LIB,其他仅来自CSV LIB和文件的正常打开:
I would encourage you to do this with a .csv file instead of a xls, as is a much easier format (requires 0 formatting of borders, colors, etc.).
You can use the os.listdir() function to list all files and folders in a certain directory. Check os built-in library docs for that. Then grab the string name of each file, remove the .pdf, and read your .csv file with the names and values, and the rename the file.
All the utilities needed are built-in python. Most are the os lib, other are just from csv lib and normal opening of files: