如何使用Python删除平面文件中的特定表?

发布于 2025-01-25 18:26:40 字数 531 浏览 1 评论 0原文

我试图编写一个循环,该循环浏览我的目录以查找以特定文件名('tvc')开头的文件,并删除该文件中的特定表。在以下情况下,“选择”是我想删除

尝试的

import os
path = 'C:/Users/grr/Desktop/autotranscribe/python/Matching'
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
         'TVC' in i]
    
    from openpyxl import load_workbook

wb = load_workbook(files)
if 'Opt-Ins' in wb.sheetnames:
    wb.remove(wb['Opt-Ins'])
wb.save(files)

TypeError: expected str, bytes or os.PathLike object, not list

床单之一的名称- 我只想删除特定的纸张。我该怎么做?

Im trying to write a loop that browses through my directory to find a file that starts with a particular filename('TVC') and deletes a specific sheet within that file. In the below case 'Opt-Ins' is the name of one of the sheets I want to delete

My attempt-

import os
path = 'C:/Users/grr/Desktop/autotranscribe/python/Matching'
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
         'TVC' in i]
    
    from openpyxl import load_workbook

wb = load_workbook(files)
if 'Opt-Ins' in wb.sheetnames:
    wb.remove(wb['Opt-Ins'])
wb.save(files)

TypeError: expected str, bytes or os.PathLike object, not list

I just want to delete a specific sheet. how do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无法回应 2025-02-01 18:26:40

问题在于,您正在尝试在A 文件名列表上调用load_workbook

wb = load_workbook(files)

for filename in files:
    wb = load_workbook(filename)
    ...  # rest of your code here

:看起来您确实了解循环,所以我认为这只是一个监督。编程时不要忘记休息一下。根据我的经验,当我工作太久而没有休息时间时,犯这样的错误的机会大大增加。

The problem is that you are trying to call load_workbook on a list of filenames:

wb = load_workbook(files)

You need to loop over the files, and run your code on each file in the loop:

for filename in files:
    wb = load_workbook(filename)
    ...  # rest of your code here

It looks like you do understand loops, so I assume this was just an oversight. Don't forget to take breaks when programming. In my experience, the chance of making a mistake like this greatly increases when I work for too long without a break.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文