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生成的代码都验证通过,最后加粗、加边框、加颜色、居中实现效果如下:

相关推荐
热心市民运维小孙11 小时前
Ubuntu重命名默认账户
linux·ubuntu·excel
懒编程-二两11 小时前
GPT Code Interpreter
gpt
又蓝18 小时前
使用 Python 操作 Excel 表格
开发语言·python·excel
m0_748230941 天前
Rust赋能前端: 纯血前端将 Table 导出 Excel
前端·rust·excel
Swift社区1 天前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift
neeef_se1 天前
Vue中使用a标签下载静态资源文件(比如excel、pdf等),纯前端操作
前端·vue.js·excel
℘团子এ1 天前
js和html中,将Excel文件渲染在页面上
javascript·html·excel
菠萝蚊鸭1 天前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
一只小灿灿1 天前
VB.NET在 Excel 二次开发中的全面应用
.net·excel
sunnf1 天前
DB-GPT 智谱在线模型配置
gpt