[英]How to use Pandas to delete column values for multiple excel files and save each of the files without combining them
I have multiple excel files containing several sheets.我有多个包含几张纸的 excel 文件。 I need to delete all values from specific columns eg cell E1:G200 (without deleting the columns) from a particular sheet.我需要从特定工作表中删除特定列中的所有值,例如单元格 E1:G200(不删除列)。 I need to do the same for all the files.我需要对所有文件做同样的事情。 After that I need to save all those edited files without combinig them.之后,我需要保存所有那些编辑过的文件而不合并它们。 I have done the same for one file, I need a macro so that I can apply for all the files.我对一个文件做了同样的事情,我需要一个宏,以便我可以申请所有文件。 I have prepared the code for one file which deletes the columns which I don't want.我已经为一个文件准备了代码,该文件删除了我不想要的列。 Also I need a macro for this job.我还需要一个宏来完成这项工作。
import os
import pandas as pd
from openpyxl import load_workbook
book = load_workbook('file1.xlsx')
sheet = book['sheet1']
#the update needed is to delete all cell values not the columns
#sheet.delete.cols(3,5)
book.save('file1_copy.xlsx')
this should work:这应该工作:
import os
import pandas as pd
from openpyxl import load_workbook
book = load_workbook('file1.xlsx')
sheet = book['Sheet1']
for row in sheet['C:E']:
for cell in row:
cell.value = None
book.save('file1_copy.xlsx')
please notice that normally Sheet1 is with capital S请注意,通常 Sheet1 是大写的 S
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.