博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用openpyxl 操作excel的基础
阅读量:5061 次
发布时间:2019-06-12

本文共 1982 字,大约阅读时间需要 6 分钟。

from openpyxl import Workbook'''写excelfrom openpyxl.compat import rangefrom openpyxl.utils import get_column_letterwb = Workbook()dest_filename = 'empty_book.xlsx'ws1 = wb.activews1.title = 'range names'for row in range(1, 40):    ws1.append(range(600))    ws2 = wb.create_sheet(title='pi')ws2['F5'] = 3.14#创建sheetws3 = wb.create_sheet(title='Data')for row in range(10, 20):    for col in range(27, 54):        _ = ws3.cell(column=col, row=row, value='{0}'.format(get_column_letter(col)))print(ws3['AA10'].value)wb.save(filename = dest_filename)''''''#读excelfrom openpyxl import load_workbookwb = load_workbook(filename = 'empty_book.xlsx')sheet_ranges = wb['range names']print(sheet_ranges['D18'].value)''''''#使用数字格式import datetimefrom openpyxl import Workbookwb = Workbook()ws = wb.active#set date using a Python datetimews['A1'] = datetime.datetime(2010, 7, 21)a = ws['A1'].number_formatprint(a)#猜类型wb.guess_types = True# set percentage using a string followed by the percent signws['B1'] = '3.14%'wb.guess_types = Falseprint(ws['B1'].value)print(ws['B1'].number_format)''''''#使用公式from openpyxl import Workbookwb = Workbook()ws = wb.active#添加一个简单公式ws['A1'] = '=SUM(1, 1)'wb.save('formula.xlsx')''''''#合并单元格/取消合并单元格from openpyxl.workbook import Workbookwb = Workbook()ws = wb.activews.merge_cells('E3:F3')ws.merge_cells('A2:D2')ws.unmerge_cells('A2:D2')#或者ws.merge_cells(start_columnt_row=2, start_column=1, end_row=4, end_column=4)ws.unmerge_cells(start_row = 2, start_column = 1, end_row = 4, end_column = 4)''''''#插入图像from openpyxl import Workbookfrom openpyxl.drawing.image import Imagewb = Workbook()ws = wb.activews['A1'] = 'You shoud see three logos below'#create on imageimg = Image('logo.png')#add to worksheet and anchor next to cellsws.add_image(img, 'A3')wb.save('logo.xlsx')'''#折叠大纲import openpyxlwb = openpyxl.Workbook()ws = wb.create_sheet()ws.column_dimensions.group('A', 'D', hidden=True)wb.save('group.xlsx')

 

转载于:https://www.cnblogs.com/nester-liz/p/9861330.html

你可能感兴趣的文章
Mongodb 基本命令
查看>>
控制文件的备份与恢复
查看>>
返回代码hdu 2054 A==B?
查看>>
iOS 8 地图
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>