简体   繁体   English

如何使用 Pandas 删除多个 excel 文件的列值并保存每个文件而不合并它们

[英]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.

相关问题 如何使用 Pandas 更新多个 excel 文件并保存更新后的文件而不合并它们 - How to use Pandas to update multiple excel files and save the updated files without combinig them 在 python pandas 中组合多个 excel 文件时出现问题 - Problem with combining multiple excel files in python pandas 为某些列Python Pandas的每个值编写多个Excel文件 - Write multiple Excel files for each value of certain column Python Pandas 如何快速读取多个excel文件,每个文件中都有多个工作表-熊猫? - How to quickly read in multiple excel files with multiple sheets in each - Pandas? 如何将 append 多个 Excel 文件中的所有工作表放入一个 Excel 文件(不将它们合并或组合成一张工作表) - How to append all sheets in multiple Excel files into One Excel file (not consolidating or combining them into one sheet) 使用pandas包在python中合并来自多个Excel文件的数据 - Combining data from multiple excel files in python using pandas package 循环 e​​xcel 文件,再添加一列并将它们保存在 Python 中 - Loop excel files, add one more column and save them in Python 将多个 excel 文件导入 python pandas 并拼接成一个 Z6A8064B5DF4794555500553C4DC7 - Import multiple excel files into python pandas and concatenate them into one dataframe 将 Excel 文件与 Pandas 中的多索引列组合 - Combining Excel Files with MultiIndexed Columns in Pandas 如何使用 python pandas (Dataframes) 从多个 excel 文件中删除前 4 行 - How to delete the first 4 rows from multiple excel files using python pandas (Dataframes)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM