- xlrd 新版本只支持 xls 格式,所以我们这里指定安装 1.2.0 老版本,可以支持xlsx格式
-
新建income.xlsx,放到代码同一目录
-
官方文档
-
案例1:计算2017年的收入
| import xlrd |
| |
| book = xlrd.open_workbook("income.xlsx") |
| |
| sheet = book.sheet_by_name('2017') |
| |
| |
| incomes = sheet.col_values(colx=1,start_rowx=1) |
| |
| print(f"2017年账面收入为: {int(sum(incomes))}") |
| |
| |
| toSubstract = 0 |
| |
| monthes = sheet.col_values(colx=0) |
| |
| for row,month in enumerate(monthes): |
| if type(month) is str and month.endswith('*'): |
| income = sheet.cell_value(row,1) |
| print(month,income) |
| toSubstract += income |
| |
| print(f"2017年真实收入为: {int(sum(incomes)- toSubstract)}") |
| import xlrd |
| |
| book = xlrd.open_workbook("income.xlsx") |
| |
| |
| sheets = book.sheets() |
| |
| incomeOf3years = 0 |
| for sheet in sheets: |
| |
| incomes = sheet.col_values(colx=1,start_rowx=1) |
| |
| toSubstract = 0 |
| |
| monthes = sheet.col_values(colx=0) |
| |
| for row,month in enumerate(monthes): |
| if type(month) is str and month.endswith('*'): |
| income = sheet.cell_value(row,1) |
| print(month,income) |
| toSubstract += income |
| |
| actualIncome = int(sum(incomes)- toSubstract) |
| print(f"{sheet.name}年真实收入为: {actualIncome}") |
| incomeOf3years += actualIncome |
| |
| print(f'全部收入为{incomeOf3years}') |
| pip install openpyxl |
| |
| xlrd 只能读取Excel内容,如果你要 创建 一个新的Excel并 写入 数据,可以使用 openpyxl 库。 |
| openpyxl 库既可以读文件、也可以写文件、也可以修改文件。 |
| 但是,openpyxl 库不支持老版本 Office2003 的 xls 格式的Excel文档,如果要读写xls格式的文档,可以使用 Excel 进行相应的格式转化。 |
| import openpyxl |
| |
| |
| book = openpyxl.Workbook() |
| |
| |
| sh = book.active |
| |
| |
| sh.title = '工资表' |
| |
| |
| book.save('信息.xlsx') |
| |
| |
| sh1 = book.create_sheet('年龄表-最后') |
| |
| |
| sh2 = book.create_sheet('年龄表-最前',0) |
| |
| |
| sh3 = book.create_sheet('年龄表2',1) |
| |
| |
| sh = book['工资表'] |
| |
| |
| sh['A1'] = '你好' |
| |
| |
| print(sh['A1'].value) |
| |
| |
| |
| sh.cell(2,2).value = '白月黑羽' |
| |
| |
| print(sh.cell(1, 1).value) |
| |
| book.save('信息.xlsx') |
| |
| import openpyxl |
| |
| name2Age = { |
| '张飞' : 38, |
| '赵云' : 27, |
| '许褚' : 36, |
| '典韦' : 38, |
| '关羽' : 39, |
| '黄忠' : 49, |
| '徐晃' : 43, |
| '马超' : 23, |
| } |
| |
| |
| book = openpyxl.Workbook() |
| |
| |
| sh = book.active |
| |
| sh.title = '年龄表' |
| |
| |
| sh['A1'] = '姓名' |
| sh['B1'] = '年龄' |
| |
| |
| row = 2 |
| |
| for name,age in name2Age.items(): |
| sh.cell(row, 1).value = name |
| sh.cell(row, 2).value = age |
| row += 1 |
| |
| |
| book.save('信息.xlsx') |
| import openpyxl |
| |
| name2Age = [ |
| ['张飞' , 38 ] , |
| ['赵云' , 27 ] , |
| ['许褚' , 36 ] , |
| ['典韦' , 38 ] , |
| ['关羽' , 39 ] , |
| ['黄忠' , 49 ] , |
| ['徐晃' , 43 ] , |
| ['马超' , 23 ] |
| ] |
| |
| |
| book = openpyxl.Workbook() |
| sh = book.active |
| sh.title = '年龄表' |
| |
| |
| sh['A1'] = '姓名' |
| sh['B1'] = '年龄' |
| |
| for row in name2Age: |
| |
| sh.append(row) |
| |
| |
| book.save('信息.xlsx') |
| import openpyxl |
| |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| |
| |
| sheet = wb['2017'] |
| |
| sheet['A1'] = '修改一下' |
| |
| |
| wb.save('income-1.xlsx') |
| import openpyxl |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| sheet = wb['2018'] |
| |
| |
| sheet.insert_rows(2) |
| |
| |
| sheet.insert_rows(3,3) |
| |
| |
| sheet.insert_cols(2) |
| |
| |
| sheet.insert_cols(2,3) |
| |
| |
| wb.save('income-1.xlsx') |
| import openpyxl |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| sheet = wb['2018'] |
| |
| |
| sheet.delete_rows(2) |
| |
| |
| sheet.delete_rows(3,3) |
| |
| |
| sheet.delete_cols(2) |
| |
| |
| sheet.delete_cols(3,3) |
| |
| |
| wb.save('income-1.xlsx') |
| import openpyxl |
| |
| from openpyxl.styles import Font,colors |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| sheet = wb['2018'] |
| |
| |
| sheet['A1'].font = Font(color=colors.RED, |
| size=15, |
| bold=True, |
| italic=True |
| ) |
| |
| |
| sheet['B1'].font = Font(color="981818") |
| |
| |
| font = Font(color="981818") |
| for y in range(1, 100): |
| sheet.cell(row=3, column=y).font = font |
| |
| |
| font = Font(bold=True) |
| for x in range(1, 100): |
| sheet.cell(row=x, column=2).font = font |
| |
| wb.save('income-1.xlsx') |
| import openpyxl |
| |
| from openpyxl.styles import PatternFill |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| sheet = wb['2018'] |
| |
| |
| sheet['A1'].fill = PatternFill("solid", "E39191") |
| |
| |
| fill = PatternFill("solid", "E39191") |
| for y in range(1, 100): |
| sheet.cell(row=2, column=y).fill = fill |
| |
| wb.save('income-1.xlsx') |
| import openpyxl |
| from openpyxl.drawing.image import Image |
| |
| wb = openpyxl.load_workbook('income.xlsx') |
| sheet = wb['2018'] |
| |
| |
| sheet.add_image(Image('1.png'), 'D1') |
| |
| |
| wb.save('income-1.xlsx') |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2022-10-07 整合springboot
2022-10-07 快速失败、非bean入参校验
2022-10-07 分组校验、级联校验、自定义验证规则