如何使用Python删除平面文件中的特定表?
我试图编写一个循环,该循环浏览我的目录以查找以特定文件名('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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您正在尝试在A 文件名列表上调用
load_workbook
:看起来您确实了解循环,所以我认为这只是一个监督。编程时不要忘记休息一下。根据我的经验,当我工作太久而没有休息时间时,犯这样的错误的机会大大增加。
The problem is that you are trying to call
load_workbook
on a list of filenames:You need to loop over the files, and run your code on each file in the loop:
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.