zoukankan      html  css  js  c++  java
  • xls0-python3my使用python.pandas修改excel样式

    xls0-python3my使用python.pandas修改excel样式


    select 6006*0.618 *0.618 x,6006*0.618 xs from DUAL ;

    select 6011*0.618 *0.618 x,6011*0.618 xs from DUAL ;


    select 6015*0.618 *0.618 x,6015*0.618 xs from DUAL ;


    select 6019*0.618 *0.618 x,6019*0.618 xs from DUAL ;


    select 6024*0.618 *0.618 x,6024*0.618 xs from DUAL ;


    declare
    i number;
    begin
    for x in 5900..6222 loop
    dbms_output.put_line(x||'--'||to_char(x*0.618,'9999.99')||'--'||round(x*0.618*0.618,3 ) );
    end loop;
    end;
    /

    SQL>

    环境:python 3.6.8

    >>> pd.read_excel('1.xlsx', sheet_name='Sheet2')
         名字   等级 属性1   属性2  天赋
    0  四九幻曦  100  自然  None  21
    1  圣甲狂战  100  战斗  None   0
    2  时空界皇  100   光    次元  27

    我们在这里使用了pd.read_excel()函数来读取excel,来看一下read_excel()这个方法的API,这里只截选一部分经常使用的参数:

    pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None)
    io:很明显, 是excel文件的路径+名字字符串

    (有中文的话python2的老铁需要使用decode()来解码成unicode字符串)
    例如:

    >>> pd.read_excel('例子'.decode('utf-8))
    sheet_name:返回指定的sheet
    如果将sheet_name指定为None,则返回全表
    如果需要返回多个表, 可以将sheet_name指定为一个列表, 例如['sheet1', 'sheet2']

    可以根据sheet的名字字符串或索引来值指定所要选取的sheet

    >>> # 如:
    >>> pd.read_excel('1.xlsx', sheet_name=0)
    >>> pd.read_excel('1.xlsx', sheet_name='Sheet1')
    >>> # 返回的是相同的 DataFrame
    name:如果没有表头, 可用此参数传入列表做表头
    header:指定数据表的表头,默认值为0, 即将第一行作为表头
    index_col:用作行索引的列编号或者列名,如果给定一个序列则有多个行索引。一般可以设定index_col=False指的是pandas不适用第一列作为行索引。

    usecols:读取指定的列, 也可以通过名字或索引值

    >>> # 如:
    >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=['等级', '属性1'])
    >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=[1,2])
    >>> # 返回的是相同的 DataFrame

    直到某一天泰格尔升了一级, 可以这样改一下, 当然用.iloc.loc对象都可以

    >>> # 读取文件
    >>> data = pd.read_excel("1.xlsx", sheet_name="Sheet1")
    
    >>> # 找到 等级 这一列,再在这一列中进行比较
    >>> data['等级'][data['名字'] == '泰格尔'] += 1
    >>> print(data)

    LOOK!他升级了!!

    >>> data
         名字   等级 属性1   属性2  天赋
    0  艾欧里娅  100  自然     冰  29
    1   泰格尔   81   电    战斗  16
    2  布鲁克克  100   水  None  28

    现在我们将它保存

    data.to_excel('1.xlsx', sheet_name='Sheet1', index=False, header=True)
    index:默认为True, 是否加行索引, 直接上图吧!
    clipboard.png
    左为False, 右为True
    header:默认为True, 是否加列标, 上图吧!
    clipboard.png
    左为False, 右为True
    io, sheet_name参数用法同函数pd.read_excel()

    如果我们多捕捉几只或者多加几种属性怎么办呢?这里给出参考:

    新增列数据:
    data['列名称'] = [值1, 值2, ......]
    >>> data['特性'] = ['瞬杀', 'None', '炎火']
    >>> data
         名字   等级 属性1   属性2  天赋    特性
    0  艾欧里娅  100  自然     冰  29    瞬杀
    1   泰格尔   80   电    战斗  16  None
    2  布鲁克克  100   水  None  28    炎火

    新增行数据,这里行的num为excel中自动给行加的id数值
    data.loc[行的num] = [值1, 值2, ...], (注意与.iloc的区别)

    >>> data.loc[3] = ['小火猴', 1, '火', 'None', 31, 'None']
    >>> data
         名字   等级 属性1   属性2  天赋    特性
    0  艾欧里娅  100  自然     冰  29    瞬杀
    1   泰格尔   80   电    战斗  16  None
    2  布鲁克克  100   水  None  28    炎火
    3   小火猴    1   火  None  31  None

    说完了增加一行或一列,那怎样删除一行或一列呢?可以使用.drop()函数

    >>> # 删除列, 需要指定axis为1,当删除行时,axis为0
    >>> data = data.drop('属性1', axis=1) # 删除`属性1`列
    >>> data
         名字   等级   属性2  天赋    特性
    0  艾欧里娅  100     冰  29    瞬杀
    1   泰格尔   80    战斗  16  None
    2  布鲁克克  100  None  28    炎火
    3   小火猴    1  None  31  None
    
    >>> # 删除第3,4行,这里下表以0开始,并且标题行不算在类, axis用法同上
    >>> data = data.drop([2, 3], axis=0)
    >>> data
         名字   等级 属性2  天赋    特性
    0  艾欧里娅  100   冰  29    瞬杀
    1   泰格尔   80  战斗  16  None
    
    >>> # 保存
    >>> data.to_excel('2.xlsx', sheet_name='Sheet1', index=False, header=True)

    参考官网提供的API:http://pandas.pydata.org/pand...

    使用python.pandas修改excel样式

    import pandas as pd
    from  datetime import datetime,timedelta
    df = pd.read_clipboard() # 从粘贴板上读取数据
    t = datetime.now().date() - timedelta(days=1)
    writer = pd.ExcelWriter('样式%d%02d%02d.xlsx' %(t.year,t.month,t.day))
    workbook = writer.book
    fmt = workbook.add_format({"font_name": u"微软雅黑"})
    percent_fmt = workbook.add_format({'num_format': '0.00%'})
    amt_fmt = workbook.add_format({'num_format': '#,##0'})
    border_format = workbook.add_format({'border': 1})
    note_fmt = workbook.add_format(
        {'bold': True, 'font_name': u'微软雅黑', 'font_color': 'red', 'align': 'left', 'valign': 'vcenter'})
    date_fmt = workbook.add_format({'bold': False, 'font_name': u'微软雅黑', 'num_format': 'yyyy-mm-dd'})
    
    date_fmt1 = workbook.add_format(
        {'bold': True, 'font_size': 10, 'font_name': u'微软雅黑', 'num_format': 'yyyy-mm-dd', 'bg_color': '#9FC3D1',
         'valign': 'vcenter', 'align': 'center'})
    highlight_fmt = workbook.add_format({'bg_color': '#FFD7E2', 'num_format': '0.00%'})
    
    l_end = len(df.index) + 2 # 表格的行数,便于下面设置格式
    df.to_excel(writer, sheet_name=u'测试页签', encoding='utf8', header=False, index=False, startcol=0, startrow=2)
    worksheet1 = writer.sheets[u'测试页签']
    for col_num, value in enumerate(df.columns.values):
        worksheet1.write(1, col_num, value, date_fmt1)
    worksheet1.merge_range('A1:B1', u'测试情况统计表', note_fmt)
    # 设置列宽
    worksheet1.set_column('A:D', 30, fmt)
    # 有条件设定表格格式:金额列
    worksheet1.conditional_format('B3:E%d' % l_end, {'type': 'cell', 'criteria': '>=', 'value': 1, 'format': amt_fmt})
    # 有条件设定表格格式:百分比
    worksheet1.conditional_format('E3:E%d' % l_end,
                                  {'type': 'cell', 'criteria': '<=', 'value': 0.1, 'format': percent_fmt})
    # 有条件设定表格格式:高亮百分比
    worksheet1.conditional_format('E3:E%d' % l_end,
                                  {'type': 'cell', 'criteria': '>', 'value': 0.1, 'format': highlight_fmt})
    # 加边框
    worksheet1.conditional_format('A1:E%d' % l_end, {'type': 'no_blanks', 'format': border_format})
    # 设置日期格式
    worksheet1.conditional_format('A3:A62', {'type': 'no_blanks', 'format': date_fmt})
    writer.save()
  • 相关阅读:
    虚拟机docker开启服务,本地无法进行访问
    make编译提示:make cc Command not found 解决办法
    yum -y install git 无法安装...提示There are no enabled repos.
    linux 安装mysql
    linux 配置环境变量
    HTML5第三天 无序有序列表、相对绝对路径
    JavaScript第一天
    HTML第二天
    mysql流程控制语句
    mysql存储过程和函数
  • 原文地址:https://www.cnblogs.com/xinxihua/p/12622951.html
Copyright © 2011-2022 走看看