Python使用策略模式和openpyxl库创建Excel文件并追加内容

python 复制代码
from openpyxl import load_workbook

# 数据数组
data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# 打开现有的 Excel 文件
excel_file = 'sheetApend_example.xlsx'
wb = load_workbook(excel_file)

# 选择要追加数据的工作表
sheet_name = 'test_Sheet2'  # 指定要追加数据的工作表名称
sheet = wb[sheet_name]

# 追加数据到工作表末尾
for row in data:
    sheet.append(row)

# 保存修改后的 Excel 文件
wb.save(excel_file)
python 复制代码
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Alignment


class ExcelManager():
    def createExcel(self, filename, sheetName,headers ):
        pass
    def appendDataToExcel(self, filename, sheetName,content ):
        pass


class CreateExcelManager(ExcelManager):
    def createExcel(self, filename, sheetName,headers ):
        # 创建一个新的Excel工作簿
        wb = openpyxl.Workbook()

        # 获取默认的活动工作表
        sheet = wb.active

        # 定义表头数据
        headers = headers

        # 将表头数据写入第一行
        sheet.append(headers)
        # 将表头数据写入第一行,并设置字体加粗
        header_row = sheet[1]
        for cell in header_row:
            cell.font = Font(bold=True)
            cell.fill = PatternFill(start_color="FFC000", end_color="FFC000", fill_type="solid")  # 设置为橙色背景
            cell.alignment = Alignment(horizontal='center', vertical='center')  # 居中对齐

        # 修改默认的工作表名称
        sheet.title = sheetName

        # 保存Excel文件
        wb.save(filename)

        print(f"Excel文件 {filename} 创建成功")

class ApendDataToExcelManager(ExcelManager):
    def appendDataToExcel(self, filename, sheetName,content ):
        # 数据数组
        # data = [
        #     [1, 2, 3],
        #     [4, 5, 6, 7],
        #     [7, 8, 9]
        # ]
        data = content
        # 打开现有的 Excel 文件
        excel_file = filename
        wb = load_workbook(excel_file)

        # 选择要追加数据的工作表
        sheet_name = sheetName  # 指定要追加数据的工作表名称
        sheet = wb[sheet_name]

        # 追加数据到工作表末尾
        for row in data:
            sheet.append(row)

        # 保存修改后的 Excel 文件
        wb.save(excel_file)

        print(f"Excel文件 {filename} 追加内容成功")



file_Name = 'excel_example.xlsx'
sheet_name = 'test_sheet1'  # 指定要追加数据的工作表名称
headers = ['A', 'B','C','D']

data = [
    [1, 2, 3],
    [4, 5, 6, 7],
    [7, 8, 9],
    ['a','b','N/A',' ','e']
]

createExcelManager = CreateExcelManager()
createExcelManager.createExcel(file_Name,sheet_name,headers)

apendDataToExcelManager = ApendDataToExcelManager()
apendDataToExcelManager.appendDataToExcel(file_Name,sheet_name,data)

Excel文件 excel_example.xlsx 创建成功

Excel文件 excel_example.xlsx 追加内容成功

相关推荐
IVEN_7 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang9 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮9 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling9 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮12 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽12 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers