欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用于操作 Excel 的 chatgpt 生成 Python openpyxl 工具包示例

最编程 2024-06-24 09:00:43
...
开源中国社区团队直播首秀,以分享为名讲述开源中国社区背后的故事”

      chatgpt 生成的一个基本的 Python Win32com 操作 Excel 的工具类示例,它包含了一些常用的单元格操作以及添加、复制和删除工作表的方法,并在代码中进行了注释来帮助您理解。

import openpyxl

class ExcelTool:
    def __init__(self, file_path):
        # 加载Excel文件
        self.filename = file_path
        self.workbook = openpyxl.load_workbook(self.filename)

    def save(self):
        # 保存更改并关闭Excel文件
        self.workbook.save(self.filename)
        self.workbook.close()

    def get_sheet_by_name(self, sheet_name):
        # 获取指定名称的工作表对象
        return self.workbook[sheet_name]

    def add_sheet(self, sheet_name):
        # 添加新的工作表
        new_sheet = self.workbook.create_sheet(sheet_name)
        return new_sheet

    def copy_sheet(self, sheet_name, new_sheet_name):
        # 复制指定名称的工作表到新的工作表
        sheet = self.get_sheet_by_name(sheet_name)
        new_sheet = self.workbook.copy_worksheet(sheet)
        new_sheet.title = new_sheet_name
        return new_sheet

    def delete_sheet(self, sheet_name):
        # 删除指定名称的工作表
        sheet = self.get_sheet_by_name(sheet_name)
        self.workbook.remove(sheet)

    def write_cell(self, sheet_name, row, column, value):
        # 写入单元格值
        sheet = self.get_sheet_by_name(sheet_name)
        sheet.cell(row=row, column=column, value=value)

    def read_cell(self, sheet_name, row, column):
        # 读取单元格值
        sheet = self.get_sheet_by_name(sheet_name)
        return sheet.cell(row=row, column=column).value

    def set_cell_format(self, sheet_name, row, column, font_size=10, bold=False, italic=False, color='000000'):
        # 设置单元格格式
        sheet = self.get_sheet_by_name(sheet_name)
        cell = sheet.cell(row=row, column=column)
        cell.font = openpyxl.styles.Font(size=font_size, bold=bold, italic=italic)
        cell.fill = openpyxl.styles.PatternFill(start_color=color, end_color=color, fill_type='solid')

    def set_range_format(self, sheet_name, start_row, start_column, end_row, end_column, font_size=10, bold=False, italic=False, color='000000'):
        # 设置范围格式
        sheet = self.get_sheet_by_name(sheet_name)
        cells_range = sheet.iter_rows(min_row=start_row, min_col=start_column, max_row=end_row, max_col=end_column)
        for row in cells_range:
            for cell in row:
                cell.font = openpyxl.styles.Font(size=font_size, bold=bold, italic=italic)
                cell.fill = openpyxl.styles.PatternFill(start_color=color, end_color=color, fill_type='solid')
使用示例:

# 创建Excel工具类实例
excel_tool = ExcelTool('test.xlsx')

# 写入单元格值
excel_tool.write_cell('Sheet1', 1, 1, 'Hello')
excel_tool.write_cell('Sheet1', 1, 2, 'World')

# 读取单元格值
value = excel_tool.read_cell('Sheet1', 1, 1)
print(value)

# 设置单元格格式
excel_tool.set_cell_format('Sheet1', 1, 1, font_size=12, bold=True, italic=True, color='FF0000')

# 添加新的工作表
new_sheet = excel_tool.add_sheet('New Sheet')

# 复制工作表
copied_sheet = excel_tool.copy_sheet('Sheet1', 'Copied Sheet')

# 删除工作表
excel_tool.delete_sheet('Sheet2')

# 保存并关闭Excel文件
excel_tool.save()