python3+pandas+pyside2拆分Excel(工作表)到不同的工作簿(Workbook)

人工智能44

背景:

公司运营部门需要将系统导出的数据(所有门店数据都在一个Sheet里)按门店(机构代码或机构名称)拆分到不同的Workbook(工作簿)文件里发给每个门店。

思路:

之前写过合并不同Workbook(工作簿)到一个Sheet(工作表)里,用python+pandas或python+xlwings,前者更好用,这次拆分就是它的逆向过程,所以在之前的基础上修改就好了。

先创建两个空文件夹一个存需拆分的源文件,一个存拆分好的结果文件,用pandas读取源文件,根据条件筛选出符合条件的数据,按门店(机构代码或机构名称)创建结果文件并保存,为方便没有编程知识的人使用,所有要借助pyside2加上操作界面,并打包成exe可执行程序。

代码:

split_excel.py 拆分excel的功能代码


import numpy as np
import pandas as pd
import xlrd
import os
import xlwings as xw

def split_sheet(source_file,result_file,condition):

    # 需拆分文件存放的文件夹
    root_path = source_file

    # 拆分结果文件存放的文件夹
    save_path = result_file

    #显示所有列
    # pd.set_option('display.max_columns', None)
    # #显示所有行
    # pd.set_option('display.max_rows', None)
    # #设置value的显示长度为100,默认为50
    # pd.set_option('max_colwidth',100)

    cont = os.listdir(root_path)
    print(cont)

    for i in range(0,len(cont)):

        # 获取需拆分文件夹路径
        root = root_path
        filename = os.path.join(root,cont[i])
        print(filename)

        df = pd.read_excel(filename,header = 0) # 读取  ,index_col = 0
        # 获取拆分依据
        df1 = df[condition]
        md = list(dict.fromkeys(list(df1)))

        # writer = pd.ExcelWriter(save_path)

        for x in md :
            # 创建保存文件路径
            save_file = os.path.join(save_path,str(x)+".xlsx")
            writer = pd.ExcelWriter(save_file)
            # 筛选
            df2 = df[df[condition]==str(x)]
            # 写入文件
            df2.to_excel(index = False , excel_writer=writer,sheet_name=str(x),startcol=0,startrow=0)
            # 保存结果
            writer.save()
            # writer.close()

    # 关闭

    writer.close()

easy.ui 用pyside2 的designer设计的操作界面文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>662</width>
    <height>516</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>300</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x5F00;&#x59CB;&#x5904;&#x7406;</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>60</y>
      <width>331</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>100</y>
      <width>331</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_3">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>180</y>
      <width>141</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>50</y>
      <width>191</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x6E90;&#x6587;&#x4EF6;&#x5939;&#x8DEF;&#x5F84;&#xFF1A;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>90</y>
      <width>161</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x7ED3;&#x679C;&#x6587;&#x4EF6;&#x5939;&#x8DEF;&#x5F84;&#xFF1A;</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>530</x>
      <y>60</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x9009;&#x62E9;</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>530</x>
      <y>100</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x9009;&#x62E9;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>160</y>
      <width>91</width>
      <height>71</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x7B5B;&#x9009;&#x6761;&#x4EF6;&#x6807;&#x9898;&#xFF1A;</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>662</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar">
 </widget>
 <resources>
 <connections>
</connections></resources></widget></ui>

pyside2_split_excel.py 将功能代码与操作界面关联起来

# # -*- coding: utf-8 -*-
from PySide2.QtWidgets import QApplication, QMessageBox,QFileDialog
from PySide2.QtUiTools import QUiLoader
import split_excel
from time import sleep
from threading import  Thread
class Stats:

    def __init__(self):
        # 从文件中加载UI定义

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意:里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('./easy.ui')
        self.filePath = self.ui.pushButton_2.clicked.connect(self.send_signal1)
        self.filePath2 = self.ui.pushButton_3.clicked.connect(self.send_signal2)

        self.ui.pushButton.clicked.connect(self.satrt_split)

    def send_signal1(self):

        filePath = QFileDialog.getExistingDirectory(self.ui, "选择【源文件】存储路径")
        self.ui.lineEdit.setText(filePath)

    def send_signal2(self):

        filePath2 = QFileDialog.getExistingDirectory(self.ui, "选择【结果文件】存储路径")
        self.ui.lineEdit_2.setText(filePath2)

    def satrt_split(self):
        self.ui.pushButton.setEnabled(False)

        def workerThreadFunc():
            source_file = self.ui.lineEdit.text()
            result_file = self.ui.lineEdit_2.text()
            condition = self.ui.lineEdit_3.text()

            split_excel.split_sheet(source_file,result_file,condition)

            self.ui.pushButton.setEnabled(True)
        worker = Thread(target=workerThreadFunc)
        worker.start()

app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()

打包好的程序已上传CSDN:

Original: https://blog.csdn.net/CSDN1120628290/article/details/123362963
Author: csdn1120628290
Title: python3+pandas+pyside2拆分Excel(工作表)到不同的工作簿(Workbook)



相关阅读

Title: Tensorflow 2.9.1安装笔记

CPU:i7-4790k

显卡:GTX2060

Cuda 版本:11.3

Cunn版本: 11.6

Python版本:3.7.7

不想用anacoda,直接装 tensorflow

1.准备工作

  • 安装python3.7.7(之前安装好的)

可以根据需要安装相应的版本,不建议安装最新版,python版本之间的代码兼容度不好。3.6~3.8可能比较合适。

我安装的是11.3版本。

deviceQuery.exe 和 bandwithTest.exe测试通过。

  • 下载Tensorflow

我下载的是 tensorflow-2.9.1-cp37-cp37m-win_amd64.whl

  • 安装组件

安装Tensorflow之前,安装好以下支持模块

A.Numpy: pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

B.mkl: pip install mkl -i https://pypi.tuna.tsinghua.edu.cn/simple

C.protobuf pip install protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple

2.安装Tensorflow

把 tensorflow-2.9.1-cp37-cp37m-win_amd64.whl 放到d盘根目录,打开命令行并转到D:\

pip install tensorflow-2.9.1-cp37-cp37m-win_amd64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple

这样在安装过程中加载其他模块的时候,速度会非常快。

3.测试

import tensorflow as tf
print("Tensorflow version:")
print(tf.__version__)

print("CPU/GPU devices for Tensorflow:")
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus)
print(cpus)

运行结果:

Tensorflow version:
2.9.1
CPU/GPU devices for Tensorflow:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

至此安装完毕。

IDE可以使用Visual Studio Code(小规模测试)

或者Pycharm(程序较大很方便)

Original: https://blog.csdn.net/st01lsp/article/details/125294794
Author: st01lsp
Title: Tensorflow 2.9.1安装笔记

相关文章
Just Speak 论文精读 人工智能

Just Speak 论文精读

目录 ABSTRACT 传统GUI操作 非视觉交互 问题 目标 语音识别服务 语音控制系统 安卓可访问性api JUSTSPEAK 系统描述 语音识别 使用解析 命令执行 命令的链接 用例和讨论 对于...
基于MATLAB的语音滤波实验 人工智能

基于MATLAB的语音滤波实验

基于MATLAB的语音滤波实验 实验目的: 在Matlab环境下对语音的频谱进行处理(数字滤波)并试听效果; 在Matlab环境下对语音的抽样率进行处理(语音压缩)并试听效果 实验步骤: 一、音频文件...
操作系统学习笔记13 | 目录与文件系统 人工智能

操作系统学习笔记13 | 目录与文件系统

本文完成磁盘管理4层抽象中的最后一层抽象:目录与文件系统。达成的效果是整个磁盘抽象为我们日常所熟悉的 目录树,这个树应当能够适配不同的操作系统(是一个独立子系统),通过 目录树/文件系统 对文件的组织...
注意力机制详解 人工智能

注意力机制详解

1.特性 即插即用 在特征提取效果显著 微调模型的小技巧 2.核心思想 本质上与人类视觉选择性注意力机制类似,从众多信息中选出对当前任务目标更为关键的信息。 通过手段获取每张特征图重要性的差异 抑制无...
条件随机场(CRF)的详细解释 人工智能

条件随机场(CRF)的详细解释

条件随机场(CRF)由Lafferty等人于2001年提出,结合了最大熵模型和隐马尔可夫模型的特点,是一种无向图模型,常用于标注或分析序列资料,如自然语言文字或是生物序列。近年来在分词、词性标注和命名...