python脚本-excel批量转换为csv文件
pandas和SQL数据分析实战视频教程
https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&shareId=400000000398149


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 4 18:04:06 2016 @author: Toby,qq:231469242,原创版权
""" import csv,os,openpyxl #获取所有excel文件名
def Get_excelFileNames():
excelFiles_list=[]
for excelFilename in os.listdir('.'):
if excelFilename.endswith('.xlsx') or excelFilename.endswith('.xls'):
excelFiles_list.append(excelFilename) return excelFiles_list '''
excelFiles_list
Out[17]: ['Book1.xlsx', 'Book2.xlsx', 'Book3.xlsx', 'Book4.xlsx'] ''' #获取一个excel内所有表格
def Get_sheets_from_oneExcel(excelFileName):
wb=openpyxl.load_workbook(excelFileName)
sheets=wb.get_sheet_names() return sheets '''
Get_sheets_from_oneExcel('Book1.xlsx')
Out[19]: ['Sheet1', 'Sheet2', 'Sheet3']
''' #获取excel的行信息
def Get_sheet_rowData(excelFileName,sheetName,rowNum):
wb=openpyxl.load_workbook(excelFileName)
sheet=wb.get_sheet_by_name(sheetName)
highest_column=sheet.get_highest_column()
rowData=[] #append each cell to this list
#loop through each cell in the row
for colNum in range(1,highest_column+1):
#append each cell's data to rowData
cell_value=sheet.cell(row=rowNum,column=colNum).value
rowData.append(cell_value) return rowData '''
Get_sheet_rowData('Book1.xlsx','Sheet1',1)
Out[39]: ['fsdf ', 'ds', 'fdf'] ''' #获取excel的信息
def Get_sheet_Data(excelFileName,sheetName):
wb=openpyxl.load_workbook(excelFileName)
sheet=wb.get_sheet_by_name(sheetName)
highest_row=sheet.get_highest_row()
highest_column=sheet.get_highest_column() sheet_data=[]
for rowNum in range(1,highest_row+1):
rowData=[] #append each cell to this list
#loop through each cell in the row
for colNum in range(1,highest_column+1):
#append each cell's data to rowData
cell_value=sheet.cell(row=rowNum,column=colNum).value
rowData.append(cell_value) sheet_data.append(rowData)
rowData=[] #清空行数据,为遍历重新准备
return sheet_data '''
Get_sheet_Data("Book1.xlsx",'Sheet1')
Out[41]: [['fsdf ', 'ds', 'fdf'], ['fdsf', 'tt', 'fds'],
['gfdgg', 'gfdg', 'gdfgdg']] ''' #取一个CSV名字
def Get_csvFileName(excelFileName,sheetName):
baseName=os.path.splitext(excelFileName)[0]
csvFileName=os.path.join('csvFiles',baseName+"_"+sheetName+".csv")
return csvFileName #把一个excel的sheet转换为csv
def Convert_oneExcelsheet_to_csv(excelFileName,sheetName):
csvFileName=Get_csvFileName(excelFileName,sheetName)
csvObj=open(csvFileName,'w',newline='')
csvWriter=csv.writer(csvObj)
sheet_data=Get_sheet_Data(excelFileName,sheetName)
for rowData in sheet_data:
csvWriter.writerow(rowData) csvObj.close() '''
Convert_oneExcelsheet_to_csv("Book1.xlsx",'Sheet1') ''' #把一个excel的所有sheet转换为各自的csv文件
def Convert_oneExcelAllsheets_to_csv(excelFileName):
sheets=Get_sheets_from_oneExcel(excelFileName)
for sheetName in sheets:
Convert_oneExcelsheet_to_csv(excelFileName,sheetName) #转换所有excel的所有sheet为各自的csv文件
def Convert_allExcellAllsheets_to_csv(excelFiles_list):
for excelFileName in excelFiles_list:
Convert_oneExcelAllsheets_to_csv(excelFileName) os.makedirs('csvFiles', exist_ok=True)
excelFiles_list=Get_excelFileNames()
#转换所有excel的所有sheet为各自的csv文件
Convert_allExcellAllsheets_to_csv(excelFiles_list)
https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149( 欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章)

python脚本-excel批量转换为csv文件的更多相关文章
- zabbix3.4用Python脚本Excel批量导入主机
1.安装xlrd读取Excel文件 1.1. 下载setuptools-38.2.4.zip,上传至zabbix服务器解压安装,下载地址:https://pypi.python.org/package ...
- Python脚本:批量将.doc文件转化为.docx文件
将.doc转换为.docx文件有几种常用的方法: Microsoft Word 和 WPS 自带.doc转换.docx功能,但只能一个文件一个文件转换,批量转换要会员 在线网页 Office-Conv ...
- excel批量转换为CSV格式,xls批量导出csv格式
工具/原料 excel 2013 地址链接:http://pan.baidu.com/s/1c1ZABlu 密码:d3rc 方法/步骤 首选我们把需要导出为CVS的Excel文件整理集中到 ...
- C# .csv文件转为Excel格式;Excel格式转换为.csv
using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Windo ...
- Python脚本打包成exe执行文件
需求 一个教辅目录结构检查工具,目录结构是[书籍]-[章节]-[题目|答案]-[*.jpg],后台有个异步处理的服务,需要强依赖这个目录结构. 书籍解析是单独的pipeline,日志对用户不可见,这里 ...
- Python:使用pymssql批量插入csv文件到数据库测试
并行进程怎么使用? import os import sys import time def processFunc(i): time.sleep(10-i) print i if __name__= ...
- Linux 用 shell 脚本 批量 导入 csv 文件 到 mysql 数据库
前提: 每个csv文件第一行为字段名 创建的数据库字段名同csv 文件的字段名 1. 批量导入 多个 csv 文件 for file in ./*.csv;do mv $file tablename. ...
- 如何使用python把json文件转换为csv文件
@ 目录 了解json整体格式 转换格式 提取key和value 使用pandas写入csv 了解json整体格式 这里有一段json格式的文件,存着全球陆地和海洋的每年异常气温(这里只选了一部分): ...
- shell脚本和python脚本实现批量ping IP测试
先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.130.2 192.168.130.3 ...
随机推荐
- Flutter——TabBar组件(顶部Tab切换组件)
TabBar组件的常用属性: 属性 描述 tabs 显示的标签内容,一般使用 Tab 对象,也可以是其他的Widget controller TabController 对象 isScrollabl ...
- Innodb关键特性之自适用Hash索引
一.索引的资源消耗分析 1.索引三大特点 1.小:只在一个到多个列建立索引 2.有序:可以快速定位终点 3.有棵树:可以定位起点,树高一般小于等于3 2.索引的资源消耗点 1.树的高度,顺序访问索引的 ...
- P1280 尼克的任务[区间覆盖dp]
题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为N分钟,从第一分钟开始 ...
- <你们都是魔鬼吗>第二次团队作业:团队项目选题
第二次团队作业:团队项目选题 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 任务1: 团队初选项目可行性自评,使用 ...
- Robot Framework--标签Tag
Robot Framework的标签是一个简单而又强大的分类机制,功能如下: 标签在reports,logs以及测试数据中展示,显示关于测试用例的元数据信息 用例的执行统计(total,passed, ...
- python----PySnooper获取打印日志
官网链接:https://pypi.org/project/PySnooper/ 安装:pip install PySnooper 使用方式,直接 导入import pysnooper,添加装饰器 ...
- prometheus监控redis,redis-cluster
Prometheus监控redis使用的是redis_exporter, 作者GitHub: https://github.com/oliver006/redis_exporter 需要说明的是: r ...
- js闭包随记
理解闭包可以将以上代码分解如下: function outerFunction() { ; function innerFunction(){ return count ...
- python - Flask 基础 - 蓝图( Blueprint )(2)
""" 蓝图:为开发者提供的目录结构 - 使用: 1. 根目录创建一个跟项目名一样的文件 - 创建后第一步,在这个文件夹中添加一个 __init__.py 的配置文件 - ...
- Nginx 负载均衡条件下 Redis 共享Session (Java)(二)
关于Session的问题 网上有各个方面的针对,有在nginx 上处理: ip_hash的session,有在 tomcat 做处理:修改Context文件,有针对项目做处理.本篇就是对项目处理 1. ...