鱼C论坛

 找回密码
 立即注册
查看: 1678|回复: 11

工作簿单元格格式转换求教~~

[复制链接]
发表于 2024-3-19 18:58:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
现有需求如下: (要求PySide6)

1、根据用户指定的工作簿后缀(xls或者xlsx), 用不同的命令打开此工作簿;

2、读取所指定工作簿的所有sheet名,用下拉框让用户指定sheet;

3、读取所指定sheet的所有列名称(可能不一定是第一行,因为有时第一行是工作簿的名称,这个需要怎么处理我还没想好),用下拉框让用户指定要转换格式的列名称;

4、在此sheet中增加一列,命名为“转换后列”;

5、对比用户所指定的列的单元格格式,用下拉框让用户指定转换后的单元格格式(自动屏蔽掉所指定列的本身格式);

6、如果转换成功,把转换后的数据写入“转换后列”中;

7、询问用户是否继续转换,否则退出。

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-3-19 19:11:56 | 显示全部楼层
FishC 发表于 2024-3-19 18:58
这个任务可以分为几个步骤来实现。首先,我们需要使用`pandas`库来读取Excel文件,用`openpyxl`库来处理Exc ...

这报错是什么意思?
Traceback (most recent call last):
  File "D:\Python\pythonProject\Xtt_xls\Excel_Change.py", line 1, in <module>
    import pandas as pd
  File "D:\Python\pythonProject\.venv\Lib\site-packages\pandas\__init__.py", line 29, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-19 19:20:45 | 显示全部楼层
FishC 发表于 2024-3-19 19:12
这个错误通常是由于在使用`pandas`库之前,在导入`numpy`库时发生了问题。根据错误信息,它建议您不要 ...

import pandas as pd
from openpyxl import load_workbook
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QComboBox, QMessageBox, QPushButton


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("工作簿格式转换")
        self.setGeometry(100, 100, 400, 300)

        self.file_combo = QComboBox(self)
        self.file_combo.setGeometry(50, 50, 300, 30)

        self.sheet_combo = QComboBox(self)
        self.sheet_combo.setGeometry(50, 100, 300, 30)

        self.column_combo = QComboBox(self)
        self.column_combo.setGeometry(50, 150, 300, 30)

        self.format_combo = QComboBox(self)
        self.format_combo.setGeometry(50, 200, 300, 30)

        self.convert_button = QPushButton("转换", self)
        self.convert_button.setGeometry(150, 250, 100, 30)
        self.convert_button.clicked.connect(self.convert)

        self.load_files()
        self.file_combo.currentIndexChanged.connect(self.load_sheets)
        self.sheet_combo.currentIndexChanged.connect(self.load_columns)

    def load_files(self):
        self.file_combo.clear()
        # 在这里根据用户输入的文件后缀来加载文件
        # 这里只是示例,假设用户输入的后缀为".xls"
        filenames = ["file1.xls", "file2.xls", "file3.xls"]
        self.file_combo.addItems(filenames)

    def load_sheets(self):
        self.sheet_combo.clear()
        # 在这里读取用户指定文件的所有sheet名
        filename = self.file_combo.currentText()
        sheets = pd.read_excel(filename, sheet_name=None).keys()
        self.sheet_combo.addItems(sheets)

    def load_columns(self):
        self.column_combo.clear()
        # 在这里读取用户指定sheet的所有列名称
        filename = self.file_combo.currentText()
        sheetname = self.sheet_combo.currentText()
        df = pd.read_excel(filename, sheet_name=sheetname)
        columns = df.columns.tolist()
        self.column_combo.addItems(columns)

    def convert(self):
        # 获取用户选择的转换后的单元格格式
        format_str = self.format_combo.currentText()

        # 在这里进行格式转换并写入到转换后列中
        filename = self.file_combo.currentText()
        sheetname = self.sheet_combo.currentText()
        column_name = self.column_combo.currentText()

        df = pd.read_excel(filename, sheet_name=sheetname)

        # 进行单元格格式转换,假设将所有数据转换为文本格式
        df["转换后列"] = df[column_name].astype(str)

        # 使用openpyxl库来保存格式转换后的数据
        with pd.ExcelWriter(filename, engine="openpyxl") as writer:
            writer.book = load_workbook(filename)
            df.to_excel(writer, sheet_name=sheetname, index=False)

        QMessageBox.information(self, "转换成功", "格式转换成功!")


if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

这段代码报错:

Traceback (most recent call last):
  File "D:\Python\pythonProject\Xtt_xls\Excel_Change.py", line 1, in <module>
    import pandas as pd
  File "D:\Python\pythonProject\.venv\Lib\site-packages\pandas\__init__.py", line 29, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-19 19:22:41 | 显示全部楼层
FishC 发表于 2024-3-19 19:21
根据报错信息,这个错误与导入`numpy`库有关。您可以尝试以下几种解决方法:

1. 确保您已正确安装了 ...

关键是这段代码并没有引用numpy啊。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-19 19:23:47 | 显示全部楼层
FishC 发表于 2024-3-19 19:22
非常抱歉,我之前的回答有误。根据您提供的代码,您需要确保已正确安装了`pandas`和`openpyxl`两个库。 ...

`pandas`和`openpyxl`库也没有问题。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-19 19:27:17 | 显示全部楼层
FishC 发表于 2024-3-19 19:24
非常抱歉给您带来困扰。我注意到我之前的回答中确实有误。实际上,在这段代码中,您不需要引入`numpy` ...

def load_files(self):
        self.file_combo.clear()
        # 在这里根据用户输入的文件后缀来加载文件
        filenames = # 根据用户输入的工作簿后缀加载文件并获取文件名列表
        self.file_combo.addItems(filenames)
工作簿要由用户来指定,再根据所指定的后缀(xls或者xlsx)打开工作簿
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-21 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表