博客
关于我
python与excel
阅读量:797 次
发布时间:2023-03-09

本文共 2772 字,大约阅读时间需要 9 分钟。

Python与Excel的结合使用在数据处理和分析中非常常见。本文将详细介绍如何利用Python脚本对Excel文件进行数据提取和图像处理。

1. 读取Excel文件

首先,我们需要读取目标Excel文件。使用xlrd库可以轻松实现这一步骤:

import xlrddata = xlrd.open_workbook(r"test.xlsx")sheet1 = data.sheet_by_name(u'Images')row = sheet1.nrowscol = sheet1.ncols

2. 提取文件名并生成路径

通过遍历Excel表格中的行和列,我们可以提取每行的名称和每列的名称,然后组合成图片文件名:

row_name_list = []col_name_list = []file_name_list = []row_index_list = []col_index_list = []for i in range(2, row):    row_name_list.append(sheet1.cell(i, 0).value.strip())    row_index_list.append(i)for i in range(1, col):    col_name_list.append(sheet1.cell(1, i).value.strip())    col_index_list.append(i)file_insert_pos = []file_path_list = []dir_path = r"E:\0629 blue"  # 请根据实际路径调整for i in row_name_list:    for j in col_name_list:        file_name = i + j + '.bmp'        file_name_list.append(file_name)for i in row_index_list:    for j in col_index_list:        file_insert_pos.append((i, j))for i in file_name_list:    file_path = os.path.join(dir_path, i)    file_path_list.append(file_path)

3. 插入图片到Excel中

接下来,我们将生成的路径和位置信息用于在Excel中插入图片。使用xlsxwriter库可以实现这一功能:

import xlsxwriterbook = xlsxwriter.Workbook('new_res.xlsx')sheet = book.add_worksheet(u'Images')for i in range(2, 50):    sheet.set_row(i, 200)sheet.set_column('B:O', 20)for i in range(len(file_path_list)):    img_path = file_path_list[i]    insert_pos = file_insert_pos[i]    sheet.insert_image(insert_pos[0], insert_pos[1], img_path, {'x_scale': 0.21, 'y_scale': 0.2})

4. 使用GUI实现文件选择

如果需要更便捷的操作,可以通过GUI实现文件选择。以下是使用PyQt5实现的GUI代码:

import sysimport osfrom PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QFileDialog, QMessageBox)class FramePane(QWidget):    def __init__(self):        super().__init__()        self.setWindowTitle("Major")        self.resize(1400, 900)        self.setup_ui()    def setup_ui(self):        self.btn1 = QPushButton(self)        self.btn1.setText("选择excel文件")        self.btn1.move(100, 100)        self.btn1.clicked.connect(self.select_excel_file)    def select_excel_file(self):        fileName_choose, filetype = QFileDialog.getOpenFileName(self,                                                                "选取文件",                                                                os.getcwd(),                                                                "All Files (*);;Text Files (*.txt)")        if fileName_choose == "":            print("\n取消选择")            return        from utils import gen_excel        res = gen_excel(fileName_choose)        mess = QMessageBox(self)        mess.setText(res)        mess.show()if __name__ == '__main__':    app = QApplication(sys.argv)    window = FramePane()    window.show()    sys.exit(app.exec_())

以上代码实现了从Excel文件中提取数据,生成图片文件名和路径,并将图片插入到新的Excel文件中。通过GUI界面,用户可以方便地选择需要处理的Excel文件。整个流程自动化,节省了大量手动操作时间。

转载地址:http://dnlfk.baihongyu.com/

你可能感兴趣的文章
Python|AsyncIO|TypeError:应为协程
查看>>
python——random模块的应用
查看>>
Python——基础知识细节
查看>>
Python——正则表达式
查看>>
Python—面向对象2
查看>>
Python“和“带整数的运算符
查看>>
Python“文件存在“制作目录时出错
查看>>
Python“文件存在“制作目录时出错
查看>>
Python、IMAP 和 GMail.将消息标记为已查看
查看>>
python一个一个获取迭代器的元素
查看>>
python一维数组 提取_python – 如何从numpy中的1维数组中提取零维切片
查看>>
python三大库之--numpy(一)
查看>>
python三大库之--numpy(二)
查看>>
python上的字无效字符_标识中的无效字符
查看>>
python下用decode和encode 中文转码
查看>>
Python下调用Linux的Shell命令
查看>>
Python接口自动化测试实战系列
查看>>
python下载_安装_配置_以及第一行python程序---python工作笔记009
查看>>
Python不协调机器人消息.内容
查看>>
Python与C#之间内存共享互传信息
查看>>