python3操作excel csv、xls、xlsb、xlsm、xlsx
生活随笔
收集整理的這篇文章主要介紹了
python3操作excel csv、xls、xlsb、xlsm、xlsx
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
語言:python3
csv
import csvdef read_csv(file_path): #csvresualt = ''f = csv.reader(open(file_path,encoding='utf-8'))for item in f:for i in item:resualt = resualt + ireturn resualt.replace(' ','').encode('GBK', 'ignore').decode('GBK') #忽略GBK不能讀取的字段filepath = '***\測試.csv' print(read_csv(filepath))xls
import xlrd def read_xls(file_path): #xlsresault = ''excel = xlrd.open_workbook(file_path)for i in range(len(excel.sheet_names())): # 獲取excel里的工作表sheet名稱數組sheet = excel.sheet_by_index(i) # 根據下標獲取對應的sheet表for i in range(0, sheet.nrows):row_list = sheet.row_values(i) # 每一行的數據在row_list 數組里resault = resault + ''.join(row_list)return resault.replace(' ','')filepath = '***\測試.xlsb' print(read_xls(filepath))xlsb
import pyxlsb def read_xlsb(file_path): xlsbresault = ''wb = pyxlsb.open_workbook(file_path)print(wb.get_sheet(wb.sheets[0]).rows())for i in range(len(wb.sheets)): # 循環遍歷所有sheetsheet_row = wb.get_sheet(wb.sheets[i]).rows()for sr in sheet_row:for s in sr:resault = resault+s.vreturn resault.replace(' ', '')filepath = '***\測試.xlsb' print(read_xlsb(filepath))xlsm、xlsx
import openpyxl def read_excel(file_path): #xlsx、xlsmresault = ''wb = openpyxl.load_workbook(file_path)sheets = wb.sheetnames # 獲取workbook中所有的表格for i in range(len(sheets)): # 循環遍歷所有sheetsheet = wb[sheets[i]]for r in range(1, sheet.max_row + 1):if r == 1:resault = resault+ '\n'+''.join([str(sheet.cell(row=r, column=c).value).ljust(17) for c in range(1, sheet.max_column + 1)])else:resault = resault+ ''.join([str(sheet.cell(row=r, column=c).value).ljust(20) for c in range(1, sheet.max_column + 1)])return resault.replace(' ','')filepath = '***\測試.xlsm' print(read_excel(filepath))整合版
import csv import openpyxl import xlrd import pyxlsbdef read_excel(file_path):result = ''if file_path[-4:] == '.csv':f = csv.reader(open(file_path, encoding='utf-8'))for item in f:for i in item:result = result + ireturn result.replace(' ', '').encode('GBK', 'ignore').decode('GBK') # 忽略GBK不能讀取的字段elif file_path[-4:] == '.xls':excel = xlrd.open_workbook(file_path)for i in range(len(excel.sheet_names())): # 獲取excel里的工作表sheet名稱數組sheet = excel.sheet_by_index(i) # 根據下標獲取對應的sheet表for i in range(0, sheet.nrows):row_list = sheet.row_values(i) # 每一行的數據在row_list 數組里result = result + ''.join(row_list)return result.replace(' ', '')elif file_path[-5:] == '.xlsb':wb = pyxlsb.open_workbook(file_path)print(wb.get_sheet(wb.sheets[0]).rows())for i in range(len(wb.sheets)): # 循環遍歷所有sheetsheet_row = wb.get_sheet(wb.sheets[i]).rows()for sr in sheet_row:for s in sr:result = result + s.vreturn result.replace(' ', '')elif file_path[-5:] in ('.xlsm','.xlsx'):wb = openpyxl.load_workbook(file_path)sheets = wb.sheetnames # 獲取workbook中所有的表格for i in range(len(sheets)): # 循環遍歷所有sheetsheet = wb[sheets[i]]for r in range(1, sheet.max_row + 1):if r == 1:result = result + '\n' + ''.join([str(sheet.cell(row=r, column=c).value).ljust(17) for c in range(1, sheet.max_column + 1)])else:result = result + ''.join([str(sheet.cell(row=r, column=c).value).ljust(20) for c in range(1, sheet.max_column + 1)])return result.replace(' ', '')filepath = '***\測試.xlsx' print(read_excel(filepath))?
?
?
總結
以上是生活随笔為你收集整理的python3操作excel csv、xls、xlsb、xlsm、xlsx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么根据css类将html转换成数组,J
- 下一篇: 计网实验一 做网线实现对等网,进行文件共