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

人工智能93

背景:

公司运营部门需要将系统导出的数据(所有门店数据都在一个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安装笔记