如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
生成demo可以看这里
python- excel 创建/写入/删sheet+花式遍历
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 输入等线 24号,加粗斜体,字体颜色红色。
# 直接使用cell的font属性,将Font对象赋值给它
# 声明样式
bold_italic_24_font = Font(name='等线', size=24, italic=True, color=Color(rgb="FF0000"), bold=True)
# 给单元格设置样式
newSheet['A1'].font = bold_italic_24_font
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 输入等线 24号,加粗斜体,字体颜色红色。
# 直接使用cell的font属性,将Font对象赋值给它
# 声明样式
bold_italic_16_font = Font(name='宋体', size=16, italic=True, color=Color(rgb="FF0000"), bold=True)
# 给第二行设置样式
for cell in newSheet[2]:
cell.font = bold_italic_16_font
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 给第三行设置对齐方式:给第三行数据设置垂直居中和水平居中
for cell in newSheet[3]:
cell.alignment = Alignment(horizontal='center', vertical='center')
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 给第四行设置行高为30
newSheet.row_dimensions[4].height = 30
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 给第五行设置列宽为15
for column_letter in range(1, newSheet.max_column + 1):
if 5 == column_letter:
# chr(64 + column_letter)用于将列号的数值转换为列字母标识符
newSheet.column_dimensions[chr(64 + column_letter)].width = 15
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 给A列设置列宽为 30
newSheet.column_dimensions['A'].width = 30
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 创建一个Border对象来定义边框样式
border = Border(
left=Side(style='thin'), # 左边框
right=Side(style='thin'), # 右边框
top=Side(style='thin'), # 上边框
bottom=Side(style='thin') # 下边框
)
# 在A1单元格应用边框样式
newSheet['A1'].border = border
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")
import os
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, Color, Border, Side
def listdir(file_path):
paths = os.listdir(file_path)
print(f'返回指定目录【{file_path}】下的所有文件和目录名:{os.listdir(file_path)}')
return paths
if __name__ == '__main__':
path = 'D:\pythonExcel'
try:
fileName = listdir(path)
file_path = os.path.join(path, fileName[0])
# 打开已有文件
wb = load_workbook(file_path)
# 创建一个新的sheet
newSheet = wb["NewSheet"]
# 设置新sheet为活动sheet
wb.active = newSheet
# 输入等线 24号,加粗斜体,字体颜色红色。
# 直接使用cell的font属性,将Font对象赋值给它
# 声明样式
bold_italic_24_font = Font(name='等线', size=24, italic=True, color=Color(rgb="FF0000"), bold=True)
bold_italic_16_font = Font(name='宋体', size=16, italic=True, color=Color(rgb="FF0000"), bold=True)
# 创建一个Border对象来定义边框样式
border = Border(
left=Side(style='thin'), # 左边框
right=Side(style='thin'), # 右边框
top=Side(style='thin'), # 上边框
bottom=Side(style='thin') # 下边框
)
# 给单元格设置样式
newSheet['A1'].font = bold_italic_24_font
# 给第二行设置样式
for cell in newSheet[2]:
cell.font = bold_italic_16_font
# 给第三行设置对齐方式:给第三行数据设置垂直居中和水平居中
for cell in newSheet[3]:
cell.alignment = Alignment(horizontal='center', vertical='center')
# 给第四行设置行高为30
newSheet.row_dimensions[4].height = 30
# 给第五行设置列宽为15
for column_letter in range(1, newSheet.max_column + 1):
if 5 == column_letter:
# chr(64 + column_letter)用于将列号的数值转换为列字母标识符
newSheet.column_dimensions[chr(64 + column_letter)].width = 15
# 给A列设置列宽为 30
newSheet.column_dimensions['A'].width = 30
# 在A1单元格应用边框样式
newSheet['A1'].border = border
wb.save(file_path)
except Exception as e:
print(f"Exception : {e}")