GPT带我学Openpyxl操作Excel

注:以下文字大部分文字和代码由GPT生成

一、openpyxl详细介绍

Openpyxl是一个用于读取和编写Excel 2010 xlsx/xlsm/xltx/xltm文件的Python库。它允许您使用Python操作Excel文件,包括创建新的工作簿、读取和修改现有工作簿中的数据、设置单元格格式以及编写公式。Openpyxl提供了丰富的功能,包括对工作表、单元格、图表和样式的操作,使得处理Excel文件变得简单而高效。

二、基本操作

2.1 openpyxl 修改字体

python 复制代码
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Apply font and alignment to all cells
for row in sheet.iter_rows():
    for cell in row:
        cell.font = Font(name='Arial', size=12, bold=True)
        cell.alignment = Alignment(horizontal='center', vertical='center')

# Save the workbook
workbook.save('your_file.xlsx')

2.2 openpyxl 加边框

python 复制代码
from openpyxl import load_workbook
from openpyxl.styles import Border, Side

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Create a border style
border_style = Border(left=Side(style='thin'), 
                      right=Side(style='thin'), 
                      top=Side(style='thin'), 
                      bottom=Side(style='thin'))

# Apply border to all cells
for row in sheet.iter_rows():
    for cell in row:
        cell.border = border_style

# Save the workbook
workbook.save('your_file.xlsx')

2.3 openpyxl 文字居中

python 复制代码
from openpyxl import load_workbook
from openpyxl.styles import Alignment

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Apply center alignment to all cells
for row in sheet.iter_rows():
    for cell in row:
        cell.alignment = Alignment(horizontal='center', vertical='center')

# Save the workbook
workbook.save('your_file.xlsx')

2.4 openpyxl 调整列宽

python 复制代码
from openpyxl import load_workbook

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Adjust column widths
sheet.column_dimensions['A'].width = 20  # Adjust the width of column A to 20
sheet.column_dimensions['B'].width = 30  # Adjust the width of column B to 30

# Save the workbook
workbook.save('your_file.xlsx')

2.5 openpyxl 数字只显示小数点后4位

python 复制代码
from openpyxl import load_workbook

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Set the number format to display only four decimal places
for row in sheet.iter_rows():
    for cell in row:
        cell.number_format = '0.0000'  # Display only four decimal places

# Save the workbook
workbook.save('your_file.xlsx')

2.6 openpyxl 改变背景色

python 复制代码
from openpyxl import load_workbook
from openpyxl.styles import PatternFill

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Set the background color to green for all cells
green_fill = PatternFill(start_color='00FF00', end_color='00FF00', fill_type='solid')
for row in sheet.iter_rows():
    for cell in row:
        cell.fill = green_fill

# Save the workbook
workbook.save('your_file.xlsx')

2.7 openpyxl 读取值某行某列值

python 复制代码
from openpyxl import load_workbook

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Read the value of a specific cell
value = sheet.cell(row=1, column=1).value  # Replace row and column with the desired cell coordinates

# Print the value
print(value)

2.8 openpyxl 写入某行某列值

python 复制代码
from openpyxl import load_workbook

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Write a value to a specific cell
sheet.cell(row=1, column=1, value='Hello, World!')  # Replace row and column with the desired cell coordinates

# Save the workbook
workbook.save('your_file.xlsx')

2.9 openpyxl 设置文字颜色

python 复制代码
from openpyxl import load_workbook
from openpyxl.styles import Font

# Load the workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active

# Set the text color of a specific cell
cell = sheet.cell(row=1, column=1)  # Replace row and column with the desired cell coordinates
cell.value = 'Hello, World!'
cell.font = Font(color="FF0000")  # Set the text color to red

# Save the workbook
workbook.save('your_file.xlsx')

2.10 openpyxl设置公式

直接写入公式字符串即可,例如=AVERAGE(D18:D33) ,但是表格的列需要转换为字母表达

python 复制代码
# 使用以下代码将列数转换为字母
def num_to_col_letters(num):
    letters = ""
    while num > 0:
        num, remainder = divmod(num - 1, 26)
        letters = chr(65 + remainder) + letters
    return letters

三、综合效果

以上gpt生成的代码都验证通过,最后加粗、加边框、加颜色、居中实现效果如下:

相关推荐
你觉得20516 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
CodeCraft Studio18 小时前
Excel处理控件Spire.XLS系列教程:C# 合并、或取消合并 Excel 单元格
前端·c#·excel
云心雨禅1 天前
Vim操作指令全解析
编辑器·vim·excel
碳基学AI1 天前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义免费下载方法
大数据·人工智能·python·gpt·算法·语言模型·集成学习
*星星之火*1 天前
【GPT入门】第33 课 一文吃透 LangChain:chain 结合 with_fallbacks ([]) 的实战指南
gpt·langchain
安分小尧1 天前
[特殊字符] 使用 Handsontable 构建一个支持 Excel 公式计算的动态表格
前端·javascript·react.js·typescript·excel
hello_simon1 天前
在线小白工具,PPT转PDF支持多种热门工具,支持批量转换,操作简单,高效适合各种需求
pdf·html·powerpoint·excel·pdf转html·excel转pdf格式
Tttian6221 天前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
有趣的我1 天前
vim的操作
编辑器·vim·excel
woniu_maggie1 天前
SAP EXCEL DOI 详解
开发语言·后端·excel