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

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

一、openpyxl详细介绍

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

二、基本操作

2.1 openpyxl 修改字体

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边框

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 文字居中

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 调整列宽

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位

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 改变背景色

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 读取值某行某列值

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 写入某行某列值

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 设置文字颜色

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) ,但是表格的列需要转换字母表达

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

三、综合效果

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

原文地址:https://blog.csdn.net/qq_40602000/article/details/134721283

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_19489.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注