首页 > 编程语言 >python3读写excel之openxlpy

python3读写excel之openxlpy

时间:2022-12-02 16:11:37浏览次数:45  
标签:sheet excel value cell print copy openxlpy python3 row

pip install openpyxl

 openpyxl 读写 xlsx 文件,不处理 xls 文件

import openpyxl
import locale
import datetime
import time
import os

# 读xlsx文件
excel = openpyxl.load_workbook('./source-files/info.xlsx')
sheet = excel.active
sheet = excel.get_sheet_by_name('test')
print(list(sheet.values))  # sheet.values 生成器
print(sheet.max_column)  # 最大列数
print(sheet.max_row)  # 最大行数
print(sheet['A1'].value)
print(sheet.cell(1, 1).value)
for row in sheet.iter_rows(max_row=1):
    title_row = [cell.value for cell in row]
print(title_row)
for row in sheet.iter_rows(min_row=2, max_row=5):
    row_data = [cell.value for cell in row]
    print(row_data)

# 写xlsx文件
locale.setlocale(locale.LC_CTYPE, 'chinese')
excel = openpyxl.Workbook()
sheet = excel.active
sheet.title = "info"
sheet.append(['name', 'age', 'class', 'datetime'])
for i in range(5)[1:]:
    for j in range(4)[1:]:
        sheet.cell(row=i+1, column=j).value = i+j
    sheet.cell(row=i+1, column=4).value = datetime.datetime.now()
    sheet.cell(
        row=i+1, column=5).value = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

sheet = excel.create_sheet('sheet', 0)  # 新建一个sheet
sheet.title = 'test'
sheet.sheet_properties.tabColor = '0d6efd'
sheet.merge_cells('A2:D4')
# sheet.unmerge_cells('A2:D4')
sheet.merge_cells(start_row=2, start_column=1, end_row=2, end_column=4)
# sheet.unmerge_cells(start_row=2, start_column=1, end_row=2, end_column=4)
img = openpyxl.drawing.image.Image('./source-files/img1.png')
sheet.add_image(img, 'D4')
sheet.row_dimensions[1].height = 22
sheet.column_dimensions['A'].width = 25
sheet.cell(row=1, column=1).alignment = openpyxl.styles.Alignment(horizontal='center', vertical='center')

copy = excel.copy_worksheet(excel['info'])
copy.title = 'backup'
for cell in copy['A']:
    print(cell.value)
for cols in copy['A:C']:
    for cell in cols:
        print(cell.value)
for row in copy['1:3']:
    for cell in row:
        print(cell.value)
for row in copy.iter_rows():
    for cell in row:
        print(cell.value)
for row in copy.iter_cols():
    for cell in row:
        print(cell.value)
for row in copy.iter_rows(min_row=1, min_col=1, max_col=3, max_row=3):
    for cell in row:
        print(cell.value)
for row in copy.rows:
    for cell in row:
        print(cell.value)
for col in copy.columns:
    for cell in col:
        print(cell.value)
print(copy.max_row, copy.max_column, copy.min_row, copy.min_column)
print(excel.sheetnames)
for sheet in excel:
    print(sheet.title)

if os.path.exists('./gen-files/test.xlsx'):
    os.remove('./gen-files/test.xlsx')
excel.save('./gen-files/test.xlsx')

 

                 

标签:sheet,excel,value,cell,print,copy,openxlpy,python3,row
From: https://www.cnblogs.com/caroline2016/p/16944756.html

相关文章

  • Python3 数据结构
    Python处理大数据集可以借助Python内置数据结构:列表、元组、字典、集合等,但是一般要和pandas和Numpy等库结合起来使用。熟练掌握Python,首先就是熟练掌握Python的数......
  • extremeTable导出excel小结
    extremeTable是很老牌的东西,最近用了下,发觉还可以,但在导出excel时,要注意如下两点.1)在web.xml中增加过滤器   <filter>         <filter-name>eXtreme......
  • Excel 2016 VBA 提取单元格的中文字符
    启用开发工具方式一:【右键Sheet1】->【查看代码】方式二:【开发者工具】->【VisualBasic】Functionchinese(rngAsString)DimregxAsObjectDimstrs,str,......
  • excel读写-ddt,config配置
    一、ddt数据驱动1、什么是ddt?:(datadrivertest)数据驱动测试(就是把用例写excel,用代码调用)2、安装ddt:pipinstallddt3、python中与excel相关模块  4、读取excel ......
  • Excel技巧:如何获取IP地址的前三位并获取IP段?
    需求说明:查看服务器的一些日志发现经常有一些爆破的记录,如果我们单个赋值IP,那将是一个巨大的工程,所以我们可以通过Excel来处理;处理步骤:1.下载日志表格;2.新建一个表,输入......
  • Microsoft Excel 文件处理之xlwt库(Python)
    MicrosoftExcel文件处理之xlwt库(Python)前言博主想将格式化数据:csv、json、xml转换成excel表格,且利用C语言实现。查看了很多资料,没有找到excel操作库,就放弃了。虽然C语......
  • PowerDesigner从Excel导入表(批量)
    PowerDesigner要导入Excel,需要使用到VB语法,同时PowerDesigner集成了访问Excel的方法,VB代码如下:'开始OptionExplicitDimmdl'thecurrentmodelSetmdl=ActiveMo......
  • SpringBoot(六) - 阿里巴巴的EasyExcel
    1、依赖<!--阿里EasyExcelstart--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.7</version></depe......
  • python3 venv虚拟环境创建与安装Django
    创建虚拟环境C:\Users\Xiao>python-mvenvD:\Pythonwork\venvtest​​激活虚拟环境C:\Users\Xiao>D:\Pythonwork\venvtest\Scripts\activate(venvtest)C:\Users\Xiao>​......
  • 2020年ArcGIS (ArcMap各版本)导出shape用EXCEL打开时,属性表中文乱码问题_JGiser的博客
    打开CMD,如果是ArcMap,输入如下命令:regaddHKEY_CURRENT_USER\Software\ESRI\Desktop10.6\Common\CodePage/vdbfDefault/tREG_SZ/d936/fDesktop后面跟的是ArcGIS的......