用Python程序批量删除excel里面的图片,


前言

文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者: Rhinoceros

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

单个excel文件

读取

1 # 使用xlrd读取excel文件
2 wb = open_workbook(path + '/' + name)

 

获取每个工作表

1 # 获取当前文件的工作表(sheet)list
2 sheetList = wb.sheets()
3 ...
4 for sheet in sheetList:
5 ...

 

修改工作表

 1 # 修改工作表使用的是xlutils, 其实也可以用xlwt,
 2 # 我没有用,原因:用资料上demo,demo使用的是这个,虽然demo没有跑通
 3 from xlutils.copy import copy
 4 ...
 5 wb = open_workbook(path + '/' + name)
 6 ...
 7 # 复制原文件,因为原文件只能读取,不能写入数据,所以要复制得到一个可以写入数据的文件
 8 newwb = copy(wb)
 9 ...
10 for row in sheet.get_rows():
11     # 遍历每一行,当8列的值小于12时,就把该值改为0
12     if row[0].value < 12:
13         newsheet.write(index, 0, 0)
14 ...

 

保存

newwb.save('./data/' + name)

 

文件下的excel文件

获取文件列表

1 import os
2 os.listdir(path)

 

功能如下:

在这里插入图片描述

全部代码如下:

 1 # -*- coding: utf-8 -*-
 2 from xlrd import open_workbook
 3 from xlutils.copy import copy
 4 import os
 5  6  7 def editExl(path, name):
 8     if os.path.exists('/data'):
 9         os.removedirs("/data")
10     # 括号里放入要读取的文件的绝对路径,相对路径也可以 
11     # os.getcwd() 返回当前.py文件所在的绝对路径
12     # print(os.getcwd(), 'lujing')
13     wb = open_workbook(path + '/' + name)
14     # 获取所读取文件的第一张表单
15     # sheet = wb.sheet_by_index(0)
16     # 获取该表单的行数
17     # s = sheet.nrows
18 19 20     # 获取当前文件的工作表(sheet)list
21     sheetList = wb.sheets()
22     # print('sheetList', sheetList)
23 24 25     # 复制原文件,因为原文件只能读取,不能写入数据,所以要复制得到一个可以写入数据的文件
26     newwb = copy(wb)
27     sheetIndex = 0
28     for sheet in sheetList:
29 30 31         # 获取可写文件的第一张表单
32         newsheet = newwb.get_sheet(sheetIndex)
33         # print(newsheet, newsheet.get_rows())
34         index = 0
35         try:
36             for row in sheet.get_rows():
37                 # 遍历每一行,当8列的值小于12时,就把该值改为0
38                 # print(row)
39                 # print(row[0].value, '000000000000000')
40                 if row[0].value < 12:
41                     # print('here', index)
42                     newsheet.write(index, 0, 0)
43                     # print('after here')
44                 index = index + 1
45         except:
46             print("aaa")
47         sheetIndex = sheetIndex + 1
48 49 50     mkdir('./data')
51     newwb.save('./data/' + name)
52 53 54 def mkdir(path): 
55     folder = os.path.exists(path)
56     if not folder:
57         os.makedirs(path)
58         print('--- folder mk ---')
59     else:
60         print('--- folder exists ---')
61 62 63 def getFileList(path):
64     return os.listdir(path)
65 66 67 def editAll():
68     originPath = './origin'
69     fileList = getFileList(originPath)
70     # print(fileList)
71     for fileItem in fileList:
72         editExl(originPath, fileItem)
73 editAll()

 

相关内容

    暂无相关文章

评论关闭