Python文件PDF重命名

发布于 2025-02-07 03:37:16 字数 163 浏览 2 评论 0原文

我在文件夹中有一个.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2025-02-14 03:37:16

您将需要在Python中使用熊猫库。它具有称为pandas.read_excel的函数,对于读取Excel文件非常有用。这将返回数据框,该框架将允许您使用iLoc或其他在第一列和第二列中访问值的方法。从那里,我建议使用os.Rename(old_name,new_name),其中old_name和new_name是保留.pdf文件的路径。重命名部分的一个完整示例看起来像这样:

import os

# Absolute path of a file
old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

# Renaming the file
os.rename(old_name, new_name)

我故意省略了一个完整的解释,因为您只是问是否有可能完成任务,因此希望这将您指向正确的方向!我建议将来根据 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 use iloc or other methods of accessing the values in the first and second columns. From there, I'd recommend using os.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:

import os

# Absolute path of a file
old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

# Renaming the file
os.rename(old_name, new_name)

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.

一张白纸 2025-02-14 03:37:16

我鼓励您使用.csv文件而不是XLS进行此操作,这是一种更容易的格式(需要0格式的边框,颜色等格式)。

您可以使用OS.listDir()函数列出某个目录中的所有文件和文件夹。检查OS内置库文档。然后获取每个文件的字符串名称,删除.pdf,然后读取带有名称和值的.CSV文件,以及重命名文件。

所需的所有实用程序都是内置的Python。大多数是OS LIB,其他仅来自CSV LIB和文件的正常打开:

with open(filename) as f:
    #anything you have to do with the file here
    #you may need to specify what permits are you opening the file with in the open function

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:

with open(filename) as f:
    #anything you have to do with the file here
    #you may need to specify what permits are you opening the file with in the open function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文