from openpyxl.chart import Reference
from openpyxl.chart import LineChart
from datetime import date
from openpyxl.styles import Font
from openpyxl.styles import PatternFill
from openpyxl.styles import Border, Side
from openpyxl.styles import Alignment
import openpyxl
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet['A1'] = 111
sheet1 = workbook.create_sheet()
sheet1['B2'] = 222
sheet3 = workbook.create_sheet("第3个工作表")
sheet3['C3'] = 333
sheet3['D5'] = "333"
sheet4 = workbook.create_sheet("第4个工作表", 1)
sheet4['A1'] = '第4个工作表'
sheet4['A5'] = '第4个工作表'
data = ['python', 'java', 'C++']
for i, d in enumerate(data):
sheet4.cell(i+1, 1).value = d
sheet.append(["客户名称(必填)", "手机号码(必填)", "邮箱", "备注"])
workbook.save("./办公自动化/files/test.xlsx")
workbook = openpyxl.load_workbook(filename=r'./办公自动化/files/test.xlsx')
sheet = workbook["Sheet"]
print(workbook.sheetnames)
for sh in workbook:
print(sh.title)
sheet_name = "Sheet"
sheet = workbook[sheet_name]
print(sheet.max_row)
print(sheet.max_column)
sh = workbook.active
print(f'第二行第三列数据: {sh.cell(2, 3).value}')
print(f'第二行第三列数据: {sh['C2'].value}')
print(f'第三行到第五行的数据:{sh[3:5]}')
print(f'A2到A4的数据: {sh['A2:D2']}')
for rows in sh['A2:D2']:
for cell in rows:
print(cell.value)
print("获取整行整列数据")
print(sh[3])
print(sh['C'])
for cell in sh['C']:
print(cell.value)
for row in sheet.iter_rows(min_row=1, max_row=1, min_col=1, max_col=1):
for cell in row:
print(cell.value)
for col in sheet.iter_cols(min_row=1, max_row=1, min_col=1, max_col=1):
for cell in col:
print(cell.value)
for row in sheet.rows:
for cell in row:
print(cell.value)
for col in sheet.columns:
for cell in col:
print(cell.value)
for row in sheet.iter_rows(values_only=True):
for cell_value in row:
print(cell_value)
for col in sheet.iter_cols(values_only=True):
for cell_value in col:
print(cell_value)
for row in sheet['A1:D3']:
for cell in row:
print(cell.value)
sheet.merge_cells('A3:D3')
sheet.unmerge_cells('A3:D3')
sheet.insert_rows(3)
sheet.insert_cols(2)
sheet.insert_rows(3, 4)
sheet.insert_cols(2, 5)
sheet.delete_rows(3)
sheet.delete_cols(2)
sheet.delete_rows(3, 4)
sheet.delete_cols(2, 5)
cell = sheet['A3']
cell.value = 123546
cell.alignment = Alignment(horizontal="center", vertical="center")
sheet.merge_cells('A4:C4')
cell = sheet['A4']
cell.value = 1235467
cell.alignment = Alignment(horizontal="center", vertical="center")
Border(
left=Side(style='thick', color='00000000'),
bottom=Side(style='mediumDashed', color='00000000'),
right=Side(style='thin', color='00000000'),
top=Side(style='dashed', color='00000000'))
cell = sheet['B5']
cell.value = 12354678
cell.border = Border(
left=Side(style='thick', color='00FF0000'),
bottom=Side(style='mediumDashed', color='00FF0000'),
right=Side(style='thin', color='00FF0000'),
top=Side(style='dashed', color='00FF0000'))
cell.alignment = Alignment(horizontal="center", vertical="center")
cell = sheet['A5']
cell.value = 123
cell.fill = PatternFill('solid', fgColor="FF00FF")
cell = sheet['C5']
cell.value = 123546888
sheet['C5'].font = Font(name='微软雅黑', italic=True,
size=30, bold=True, color="00FFFF")
sheet['C6'] = 1235468889
sheet['C6'].font = Font(name='微软雅黑', u='double',
italic=True, size=30, bold=True, color="00FFFF")
sheet.row_dimensions[1].height = 60
sheet.column_dimensions["B"].width = 30
ws = workbook.create_sheet('图表')
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2020, 12, 1), 40, 30, 25],
[date(2020, 12, 2), 30, 25, 30],
[date(2020, 12, 3), 50, 26, 30],
[date(2020, 12, 4), 20, 18, 40],
[date(2020, 12, 5), 10, 16, 20],
[date(2020, 12, 6), 30, 38, 25],
]
for row in rows:
ws.append(row)
c1 = LineChart()
c1.title = 'Line Chart'
c1.x_axis.title = 'Test_number'
c1.y_axis.title = 'size'
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data,titles_from_data=True)
ws.add_chart(c1, 'A9')
workbook.save("./办公自动化/files/test.xlsx")