`

python读写excel

阅读更多
# coding=utf-8
'''
Created on 2017年3月2日

@author: chenkai
'''

import xlrd #读取excel

data =xlrd.open_workbook("F:\\test.xlsx")
table=data.sheets()[0]
nrows=table.nrows#行数
ncols=table.ncols#列数
print "行数: ",nrows,"列数: " ,ncols
for i in range(0,nrows):
    print "--------------------第",i,"行"
    rowValues=table.row_values(i)#某一行数据
    for item in rowValues:
        print item
       
       
'''
编写excel
'''
import xlwt

'''
往EXCEl单元格写内容,每次写一行sheet:页签名称;rowValueList:行内容列表;rowIndex:行索引;
isBold:true:粗字段,false:普通字体
'''

def WriteSheetRow(sheet, rowValueList, rowIndex, isBold):
    i = 0
    #style = xlwt.easyxf('font: bold 1')
    style = xlwt.easyxf('font: bold 0, color red; pattern: pattern solid, fore_colour yellow;')#红色字体
    '''
   
      # style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
    # 设置Excel单元格的背景色为黄色,字体为粗体
    '''
    for svalue in rowValueList:
        strValue = unicode(str(svalue), 'utf-8')
        if isBold:
            sheet.write(rowIndex, i, strValue, style)
        else:
            sheet.write(rowIndex, i, strValue,style)
            i = i + 1
'''写excel文件'''
def save_Excel(strFile):
    excelFile = unicode(strFile, "utf8")
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
    headList = ['标题1', '标题2', '标题3', '标题4', '总计']
    rowIndex = 0
    WriteSheetRow(sheet, headList, rowIndex, True)
    for i in xrange(1, 11):
        rowIndex = rowIndex + 1
        valueList = []
        for j in xrange(1, 5):
            valueList.append(j * i)
            WriteSheetRow(sheet, valueList, rowIndex, False)
            wbk.save(excelFile)

save_Excel("F:\\test2.xls")
'''
  style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')

  在设置上Excel单元格的背景色时,fore_colour 支持的颜色是有限的,仅支持一下颜色

  aqua 0x31
  black 0x08
  blue 0x0C
  blue_gray 0x36
  bright_green 0x0B
  brown 0x3C
  coral 0x1D
  cyan_ega 0x0F
  dark_blue 0x12
  dark_blue_ega 0x12
  dark_green 0x3A
  dark_green_ega 0x11
  dark_purple 0x1C
  dark_red 0x10
  dark_red_ega 0x10
  dark_teal 0x38
  dark_yellow 0x13
  gold 0x33
  gray_ega 0x17
  gray25 0x16
  gray40 0x37
  gray50 0x17
  gray80 0x3F
  green 0x11
  ice_blue 0x1F
  indigo 0x3E
  ivory 0x1A
  lavender 0x2E
  light_blue 0x30
  light_green 0x2A
  light_orange 0x34
  light_turquoise 0x29
  light_yellow 0x2B
  lime 0x32
  magenta_ega 0x0E
  ocean_blue 0x1E
  olive_ega 0x13
  olive_green 0x3B
  orange 0x35
  pale_blue 0x2C
  periwinkle 0x18
  pink 0x0E
  plum 0x3D
  purple_ega 0x14
  red 0x0A
  rose 0x2D
  sea_green 0x39
  silver_ega 0x16
  sky_blue 0x28
  tan 0x2F
  teal 0x15
  teal_ega 0x15
  turquoise 0x0F
  violet 0x14
  white 0x09
  yellow 0x0D
'''

   
分享到:
评论

相关推荐

    python读写excel文件

    python读写excel

    python读取excel数据绘制简单曲线图的完整步骤记录

    python读写excel文件有很多种方法: 用xlrd和xlwt进行excel读写 用openpyxl进行excel读写 用pandas进行excel读写 本文使用xlrd读取excel文件(xls,sxls格式),使用xlwt向excel写入数据 一、xlrd和xlwt的安装 安装...

    Python读取Excel文件.zip

    python读取excel数据:Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python读取Excel文件Python...

    python读写excel文件代码

    python读写excel文件代码,读写mdb代码

    Python 读取Excel、文本、CSV等不同类型数据 Python源码

    Python 读取Excel、文本、CSV等不同类型数据 Python源码Python 读取Excel、文本、CSV等不同类型数据 Python源码Python 读取Excel、文本、CSV等不同类型数据 Python源码Python 读取Excel、文本、CSV等不同类型数据 ...

    python 读写 Excel 学习总结

    Python 读写 Excel 是在数据分析、自动化报告以及数据管理中常用的一种技能。Excel 文件因其直观易用和强大的数据处理能力而备受青睐,Python 提供了多种库来方便地与 Excel 文件交互,如 `pandas`、`openpyxl`、`...

    python读取excel数据源码.zip

    python读取excel数据python读取excel数据源码.zippython读取excel数据源码.zippython读取excel数据源码.zippython读取excel数据源码.zippython读取excel数据源码.zippython读取excel数据源码.zippython读取excel数据...

    python读取Excel并写入.zip

    要读取Excel文件,可以使用`pandas`的`read_excel()`函数。例如: ```python import pandas as pd # 读取Excel文件 df = pd.read_excel('文件路径.xlsx') # 查看数据 print(df) ``` 这将把Excel文件加载为一个...

    数据预处理,python读取excel数据,分类属性数值化

    本篇文章将深入探讨如何使用Python读取Excel数据,并将分类属性数值化。 首先,我们需要引入必要的库。`pandas`库是处理结构化数据的首选,它提供了DataFrame对象,可以方便地处理Excel、CSV等格式的数据。`numpy`...

    python读取excel文件例子

    本例子将重点介绍如何使用Python通过`pandas`库来读取Excel文件。 首先,`pandas`是一个强大的数据处理库,它提供了DataFrame对象,能够方便地处理二维表格数据。要安装`pandas`,可以使用pip命令: ``` pip ...

    python读取Excel中的数据,然后写进word

    代码用于读Excel中的数据,然后批量写进word,文件的读写有专门的类,对于批量处理excel很有帮助

    Msptest-python读取excel数据

    demodemodemo python读取excel数据 python读取excel数据 python读取excel数据 python读取excel数据 python读取excel数据

    python读写excel表格脚本

    ### Python读写Excel表格脚本知识点详解 #### 一、导入必要的库 在Python中处理Excel文件时,通常会用到几个重要的库:`xlrd`用于读取Excel文件,`xlwt`或`openpyxl`用于写入Excel文件。而在本脚本中,作者选择了`...

    python读取excel数据,分组数据自动生成shell脚本,并转换为unix格式

    用pandas读取excel数据,并把数据分组保存在txt文件里,自动生成shell脚本,把window的格式转换为unix格式可直接拖到linux执行shell脚本

    Python读取excel 文件操作说明.zip_excel_morningy2h_python 处理excel_python操

    `pandas`库是Python数据分析的强大工具,而`openpyxl`则专门用于读写Excel的xlsx格式文件。 1. **安装库**: 在开始之前,确保已经安装了`pandas`和`openpyxl`。如果未安装,可以通过pip命令进行安装: ``` pip ...

    python读写Excel_MDB_sqlite2

    python读写Excel_MDB_sqlite的示例代码

    python读取excel数据.doc

    ### Python读取Excel数据知识点详解 #### 一、概述 在数据分析领域,Excel文件是非常常见的数据存储格式之一。Python作为一种强大的编程语言,在处理Excel文件方面提供了多种库支持,其中最常用的是`pandas`和`...

    Python 读取Excel数据.docx

    ### Python 读取Excel数据知识点详解 #### 一、引言 随着数据分析和自动化办公需求的日益增长,Python 作为一种强大的编程语言,在处理各种格式的数据方面展现出了非凡的能力。其中,Excel 文件因其广泛的应用场景...

    python读写excel.zip

    `pandas.read_excel()`函数是用于读取Excel文件的核心方法。它能直接将Excel文件加载为DataFrame对象,方便后续的数据分析。例如: ```python import pandas as pd # 读取Excel文件 df = pd.read_excel('your_file...

    excel_python写入excel_python读取EXCEL_pythonexcel_

    读取Excel文件同样使用`pandas`库的`read_excel`函数。例如: ```python # 读取Excel文件到DataFrame df = pd.read_excel('input.xlsx') # 打印DataFrame内容 print(df) ``` 这将打印出Excel文件中的所有...

Global site tag (gtag.js) - Google Analytics