Python - 操作Excel(中)


例子文件如下:

一些复杂的读取操作
getCells.py

import openpyxl

wb = openpyxl.load_workbook(‘example.xlsx‘)
sheet = wb.get_sheet_by_name(‘Sheet1‘)

print(sheet.cell(row=1, column=2).value)
# from 1 to 8 step is 2
for i in range(1, sheet.max_row + 1, 2):
    print(i, sheet.cell(row=i, column=2).value)

print(‘------------------------------------------------‘)

# enumerate range cells
for rowOfCellObjects in sheet[‘A1‘:‘C3‘]:
    for cellObj in rowOfCellObjects:
        print(cellObj.coordinate, cellObj.value)
    print(‘**************************************‘)

print(‘------------------------------------------------‘)

# enumerate the whole sheet
for i in range(1, sheet.max_row + 1):
    for j in range(1, sheet.max_column + 1):
        print(i, sheet.cell(row=i, column=j).coordinate, sheet.cell(row=i, column=j).value)
    print(‘**************************************‘)

运行结果:

Apples
1 Apples
3 Pears
5 Apples
7 Strawberries
------------------------------------------------
A1 2015-04-05 13:34:02
B1 Apples
C1 73
**************************************
A2 2015-04-05 03:41:23
B2 Cherries
C2 85
**************************************
A3 2015-04-06 12:46:51
B3 Pears
C3 14
**************************************
------------------------------------------------
1 A1 2015-04-05 13:34:02
1 B1 Apples
1 C1 73
**************************************
2 A2 2015-04-05 03:41:23
2 B2 Cherries
2 C2 85
**************************************
3 A3 2015-04-06 12:46:51
3 B3 Pears
3 C3 14
**************************************
4 A4 2015-04-08 08:59:43
4 B4 Oranges
4 C4 52
**************************************
5 A5 2015-04-10 02:07:00
5 B5 Apples
5 C5 152
**************************************
6 A6 2015-04-10 18:10:37
6 B6 Bananas
6 C6 23
**************************************
7 A7 2015-04-10 02:40:46
7 B7 Strawberries
7 C7 98
**************************************

时间: 2024-08-24 11:11:46

Python - 操作Excel(中)的相关文章

python 操作excel表格

Python 操作excel 表格 #coding=utf-8 import xlsxwriter #1.创建excel 对象 work = xlsxwriter.Workbook('hello.xlsk')#在当前目录下创建一个Excel文件 #2. 创建表格 worksheet = work.add_worksheet('int') #3.写入内容 title_index = ["A","B","C","D","

python基础(六)python操作excel

一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这几个模块使用pip安装即可,下面是这几个模块的使用. 二.xlrd模块,xlrd模块用来读excel,具体用法如下: import xlrd #打开excel wb=xlrd.open_workbook('abc.xlsx')#打开的这个excel必须存在,否则会报错 print(wb.shee

【转】python操作excel表格(xlrd/xlwt)

[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究"(用此一词让我觉得颇有成就感)之后,基本解决了日常所需.主要记录使用过程的常见问题及解决. python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库.可从这里下载https://pyp

自动化办公:python操作Excel

1.安装 -- upgrade pippython -m pip install --- install pypipip install pypi 执行python setup.py install进行安装xlrd Python操作Excel,主要用到xlrd和xlwt这两个库,即xlrd是读Excel,xlwt是写Excel的库 pip install xlrdpip install xlwtpip install xlutilspip install xlsxwriter 2.操作一个简单的

八、python操作excel及网络编程和异常处理

一.python操作excel 1.读excel,xlrd模块用来读excel # book = xlrd.open_workbook(r'students.xlsx')#打开excel# print(book.sheet_names())#获取所有sheet的名字# sheet = book.sheet_by_index(0)#根据sheet页的位置去取sheet# sheet2 = book.sheet_by_name('Sheet2')#根据sheet页的名字获取sheet页# print

python 操作 excel

python操作execel主要是读写 读 通过 http://pypi.python.org/pypi/xlrd 写 通过 http://pypi.python.org/pypi/xlwd 下载tar包,解压出来,安装即可, 如果没有权限,将xlrd/xlrd拷贝到当前目录下,也可以使用. 如下是xlrd的使用例子 1 # -*- coding: utf-8 -*- 2 import xdrlib ,sys 3 import xlrd 4 import sys 5 def open_excel

Python读取excel中的图片

Python读取excel中的图片文件,并转成base64 import sys import os import xlrd import zipfile import base64 class ExcelImgRead(object): def change_file_name(self, file_path, old_name, new_type = '.zip'): """ 修改指定目录下的文件类型名 :param file_path: :param old: :par

【python操作excel相关】

读取excel文件,这里使用xlrd,使用的时候首先要导入xlrd模块:得到的类型是unicode数据类型 ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error #打开excel文件读取数据 data = xlrd.open_workbook("test.xls") #获取一个工作表方式一 table = data.sheets()[0] #获取一个工作表方式二 table = data.sheet_by_inse

使用Python将Excel中的数据导入到MySQL

使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubuntu自带.使用前请使用下面的命令确保是2.7.x版本: python --version xlrd : 这是一个扩Python包,可以使用pip包管理工具安装:pip install xlrd MySQLdb 为MySQL 的Python驱动接口包,可以到http://sourceforge.net/