Python自动化应用——将Python汇总Excel数据表程序打包成exe可跨电脑执行的程序(不需要安装python)

目录

一、准备工作

二、执行代码

三、打包

四、应用

1.执行前

2.执行中

3.执行后


一、准备工作

1.创建虚拟环境,参考:https://blog.csdn.net/qq_15028721/article/details/90316152

virtualenv  虚拟环境名称

2.安装依赖包

#读取excel
pip install xlrd
#写入excel
pip install xlwt
#打包成exe
pip install pyinstaller

二、执行代码

import xlrd
import xlwt
import os

path = os.getcwd()#获取当前目录
os.chdir(path)
allfiles = os.listdir(path)
for i in allfiles:
    if os.path.splitext(i)[1] !='.xls':
        if os.path.splitext(i)[1] !='.xlsx':
            allfiles.remove(i)
#把当前目录下,所有以xls,xlsx结尾的文件录入列表
print(allfiles)
result = xlwt.Workbook(encoding='utf-8')
sheeta = result.add_sheet('result')

n = 0

for i in range(len(allfiles)):
    #1.打开表格,当i=0,打开第一个表
    f = xlrd.open_workbook(allfiles[i])
    #2.获取数据
    #通过序号获得sheet数据
    psheet = f.sheets()[0]
    #通过名字获得sheet数据,sheet_by_name(sheet_name):通过sheet名称获取所需sheet对象
    #psheet = f.sheet_by_name('明细结果')
    #3.写入数据
    t = i#t代表第几个表

    if i==0:
        # 获取标题行列数
        ncols = psheet.ncols  # 获取某sheet中的有效列数
        # rows = psheet.row_values(1)#通过一行数据的长度获取有效列数
        print('第'+ str(i) +'表列数' + str(ncols))
        # 获取表的行数
        nrows = psheet.nrows  # 获取某sheet中的有效行数
        print('第'+ str(i) +'表行数' + str(nrows))

        for ncol in range(ncols):#循环列
            sheeta.write(0, ncol, psheet.row_values(0)[ncol])
            for nrow in range(1,nrows):#循环行
                sheeta.write(n + nrow, ncol, psheet.row_values(nrow)[ncol])
        n = n + nrows-1
        print('总行数' + str(n))
    else:
        # 获取标题行列数
        ncols = psheet.ncols  # 获取某sheet中的有效列数
        # rows = psheet.row_values(1)#通过一行数据的长度获取有效列数
        print('第'+ str(i) +'表列数' + str(ncols))
        # 获取表的行数
        nrows = psheet.nrows  # 获取某sheet中的有效行数
        print('第'+ str(i) +'表行数' + str(nrows))

        for ncol in range(ncols):#循环列
            for nrow in range(1,nrows):#循环行
                sheeta.write(n + nrow, ncol, psheet.row_values(nrow)[ncol])
        n = n + nrows-1
        print('总行数' + str(n))

result.save('汇总表.xls')
#保存文件

三、打包

pyinstaller -F C:\Users\50521\PycharmProjects\untitled6\python_to_excel\main.py

四、应用

1.执行前

2.执行中

3.执行后

下载链接:https://download.csdn.net/download/qq_15028721/15651587

Guess you like

Origin blog.csdn.net/qq_15028721/article/details/114528588