import openpyxl
new_filename = 'data/normal_1000.xlsx'
wb = openpyxl.load_workbook(new_filename)
_sheet = wb['北京'] #根据sheet名字获取内容
for cell_row in _sheet['B2':'C5']:
for cell in cell_row:
print(cell.value)
#获取第一列数据
for cell in list(_sheet.columns)[0]:
print(cell.value)
import openpyxl
from openpyxl.styles import Font, PatternFill, Side, Border
wb = openpyxl.Workbook()
wb.create_sheet(index=1,title='陕西')
sheet = wb['陕西'] #获取指定的工作表
#方式一:通过单元格的形式写入
sheet['A3'] = '123'
#--------定制样式---------
sheet['A3'].font = Font(name='楷体',
italic=True, #设置斜体,
size=20,
underline='singleAccounting',
b=True,
color='FFFFFF') #字体样式
sheet['A3'].fill = PatternFill(patternType='solid',fgColor='4433FF') #背景样式
s1 = Side(style='thin',color='867677')
s2 = Side(style='double',color='ff7677')
sheet['A7'].border = Border(top=s1)
sheet['A5'].border = Border(top=s2,bottom=s2,right=s1,left=s1) #边框样式
c1 = sheet['A3'].alignment = Alignment(horizontal='center',vertical='bottom') #对齐方式
#--------定制样式---------
#方式二:指定行与列的形式写入
sheet.cell(row=2,column=3).value='345'
wb.save('./data/country7.xlsx')
#del wb['陕西'] #删除工作表,记得删除完后需要保存
#wb.save('./data/country7.xlsx')
https://www.bilibili.com/video/BV1FF411G7vd/?p=12&spm_id_from=pageDriver&vd_source=25aa9ff06474f1b6164967dce663dd94