主题
读写xlsx
python
from openpyxl import load_workbook
class Excel_XLSX:
def __init__(self,filepath):
self.filepath = filepath
self.wb = load_workbook(self.filepath)
self.ws = self.wb['Sheet1']
def read(self):
for row in self.ws.rows:
for cell in row:
print(row, cell.value)
def change_data(self,columns,values):
rows = self.ws.max_row+1
for index in range(len(columns)):
key = str(columns[index]) + str(rows)
self.ws[key] = str(values[index])
# 是否有id
def is_exists(self,item_id):
for row in self.ws.rows:
row_values = []
for cell in row:
row_values.append(cell.value)
if item_id in row_values:
return True
return False
def save(self):
self.wb.save(self.filepath)
if __name__ == '__main__':
excel_xlsx = Excel_XLSX('upload_list.xlsx')
excel_xlsx.change_data(['A','B','C','D'],["100","作品名称",'照片名称','拍摄时间','照片路径'])
print(excel_xlsx.is_exists("畅天游vr产品1-1"))
# excel_xlsx.save()
读xls
python
import xlrd
xls_path = "xxxx.xls"
data = xlrd.open_workbook(xls_path)
table = data.sheet_by_index(0)
nrows = table.nrows
for rowx in range(1,nrows):
imageName = table.row(rowx)[0].value