import openpyxl as oxl
from openpyxl.utils import get_column_letter, column_index_from_string
import winreg
import os def main():
# 加载工作簿
desktop_path = get_desktop_path()
wb = oxl.load_workbook(os.path.join(desktop_path(),'aaa.xlsx')) # 加载指定工作表
sheet = wb[wb.sheetnames[1]] # 获取指定单元格内容
c_value = sheet.cell(row=5, column=3).value
c_coordinate = sheet.cell(row=5, column=3).coordinate # 获取最大行和最大列
max_row = sheet.max_row
max_column = sheet.max_column # 列名的字母和数字转换
zm = get_column_letter(3)
sz = column_index_from_string('D') # 遍历区域内的单元格
for my_row in sheet['A1':'C9']:
print('current row is: ' + str(my_row[0].row))
for my_col in my_row:
print(my_col.coordinate + ': ' + my_col.value) # 打印表格
print_sheet(sheet) # 创建新工作簿
creat_workbook() def print_sheet(sheet):
# 生成列序号
a = list(range(1, sheet.max_column + 1))
b = list(map(get_column_letter, a)) # 打印列序号
for i in b:
print('\t' + i, end='')
print(end='\n') # 遍历工作表的所有单元
for my_row in range(0, sheet.max_row): # 逐行遍历
print(str(my_row + 1), end='') # 打印行序号
for my_col in list(sheet.rows)[my_row]: # 逐列遍历
print('\t' + my_col.value, end='') # 打印单元格
print(end='\n') # 回车 # 创建新工作簿
def creat_workbook():
# 创建新工作簿
wb = oxl.Workbook()
# 获取当前工作表
sheet = wb.active
# 更改工作表名称
sheet.title = 'sheet1'
# 创建工作表
wb.create_sheet(title='sheet2', index=0)
wb.create_sheet(title='sheet33', index=2)
# 删除工作表
wb.remove(wb[wb.sheetnames[2]])
# 修改单元格内容
sheet = wb[wb.sheetnames[1]]
for my_row in range(1, 11):
for my_col in range(1, 11):
sheet.cell(row=my_row, column=my_col).value = get_column_letter(my_col) + str(my_row)
# 保存工作簿
wb.save(os.path.join(desktop_path(),'ccc.xlsx')) # 获取当前系统的桌面绝对路径
def desktop_path():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
path = winreg.QueryValueEx(key, "Desktop")[0]
return path if __name__ == '__main__':
main()

python,openpyxl,读写excel文件的更多相关文章

  1. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  2. 【转发】Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  3. Python使用读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  4. Python如何读写Excel文件-使用xlrd/xlwt模块

    时间: 2020-08-18 整理: qiyuan 安装和导入 1.模块介绍 在 python 中使用 xlrd/xlwt 和 openpyxl 模块可以对Excel电子表格(xls.xlsx文件)进 ...

  5. Python3使用openpyxl读写Excel文件

    Python中常用的操作Excel的三方包有xlrd,xlwt和openpyxl等,xlrd支持读取.xls和.xlsx格式的Excel文件,只支持读取,不支持写入.xlwt只支持写入.xls格式的文 ...

  6. openpyxl读写Excel文件

    安装 pip install openpyxl 一个简单的实例: 最初的表格 #!/usr/bin/env python # -*- coding:utf-8 -*- import openpyxl ...

  7. 用Python读写Excel文件(转)

    原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...

  8. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  9. 用Python读写Excel文件的方式比较

    虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...

  10. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

随机推荐

  1. java里mongodb复合查询

    Query query = new Query();Criteria criteria = Criteria.where("packetTitle").is(redPacketSt ...

  2. 51nod 1346:递归

    1346 递归 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 函数f(n,m) { 若n=1或m=1返回a[n][m]; 返回f(n-1,m)异或 ...

  3. java常用第三方类库

    Guava:来自Google的常用类库 Apache Commons:来自Apache的常用类库 Mockito:主要用于单元测试的mock DBUnit:测试中管理数据库测试数据 Rest Assu ...

  4. 步进电机加减速S曲线算法

    一.Sigmoid 函数 1.1 Sigmoid函数原型 1.2 sigmoid函数波形: 由图形可看出在-10时已经接近于0,一般取值区间在[-5,5]. 1.3 sigmoid函数的导数 转载CS ...

  5. bash字符串处理

    将movie目录下的文件名写到markdown文件中 , 再转html rm index.md ; for f in `find . *.* | sort`; do [ -f $f ] &&a ...

  6. javaweb01

    Java web应用由一组servlet.HTML页,类,以及它可以被绑定的资源构成,它可以在各种供应商提供的实现servlet规范容器中运行javaweb包括这些:Servlet jsp 实用类 静 ...

  7. frp内网穿透,centos7+frp成功样例

    准备工作: 阿里云服务器一台,备案域名一个,本地服务器一台(本人用的虚拟机centos7) frp文件:frp_0.22.0_linux_amd64.tar.gz 链接:https://pan.bai ...

  8. ubuntu搭建web服务器

    https://www.linuxidc.com/Linux/2015-11/125477.htm 到“sudo apt-get install libapache2-mod-php5”出现1错误.

  9. php的date()函数判断今天是星期几

    d  月份中的第几天,有前导零的 2 位数字 01 到 31 D  星期中的第几天,文本表示,3 个字母 Mon 到 Sun j  月份中的第几天,没有前导零 1 到 31 l  ("L&q ...

  10. 吴裕雄--天生自然 PHP开发学习:表单验证

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...