![](/img/trans.png)
[英]How to use Pandas to update multiple excel files and save the updated files without combinig them
[英]How to use Pandas to delete column values for multiple excel files and save each of the files without combining them
我有多個包含幾張紙的 excel 文件。 我需要從特定工作表中刪除特定列中的所有值,例如單元格 E1:G200(不刪除列)。 我需要對所有文件做同樣的事情。 之后,我需要保存所有那些編輯過的文件而不合並它們。 我對一個文件做了同樣的事情,我需要一個宏,以便我可以申請所有文件。 我已經為一個文件准備了代碼,該文件刪除了我不想要的列。 我還需要一個宏來完成這項工作。
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')
這應該工作:
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')
請注意,通常 Sheet1 是大寫的 S
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.