python excel总结

2021/12/26 1:07:36

本文主要是介绍python excel总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、python自动化—处理excel表格

实际使用过程中:数据预处理(数据分析和建模方面)使用pandas模块;若要设置excel表格中的样式、追加数据则使用openpyxl模块

python自动化处理excel表格,主要有以下几个模块:

库名安装支持功能官网
xlwt pip install xlwt Excel:xls(2003版);.xls最多只能支持256列excel表格写入https://pypi.org/project/xlwt/
xlrdpip install xlrdExcel:xlsx(2007版)与xls(2003版)excel表格读取https://pypi.org/project/xlrd/
xlsxwriterpip install xlsxWriterExcel:xlsx(2007+)excel表格写入https://xlsxwriter.readthedocs.io/
openpyxlpip install openpyxlExcel:xlsx(2010)excel表格读写https://pypi.org/project/openpyxl/
pandaspip insatll pandasexcel、txt、csv、json、mysql数据分析和建模http://www.pypandas.cn/

1、Excel表格的几个对象

Excel表格几个对象:

  • WorkBook:工作簿对象
  • Sheet:表单对象
  • Cell:表对象
  • rows:行
  • columns:列

2、几个模块的说明

  • xlwt、xlrd、xlsxwriter、openpyxl:这几个模块是对excel表格直接操作。
  • pandas 模块是对数据预处理的模块,不局限于excel表格(官网讲解很详细)。

Python在数据处理和准备方面一直做得很好,但在数据分析和建模方面就没那么好了。Pandas帮助填补了这一空白,使您能够在Python中执行整个数据分析工作流程,而不必切换到更特定于领域的语言,如R

pandas中excel表格,可以指定对应的excel处理引擎,默认为xlsxwriter, 也可以指定openpyxl。其中xlsxwriter只能覆盖原始数据,而openpyxl是可以对数据进行增加的

    import pandas as pd
	import numpy as np
	import os
	
    out_excel = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'source/out.xlsx')
    try:
        writer = pd.ExcelWriter(out_excel, engine='openpyxl')
        df.to_excel(writer, sheet_name='student', header=True, index=True, startrow=0, startcol=0)
        writer.save()
    except Exception as e:
        print(e)
        
'''>>> df2 = df1.copy()
    >>> with pd.ExcelWriter('output.xlsx') as writer:  # doctest: +SKIP
    ...     df1.to_excel(writer, sheet_name='Sheet_name_1')
    ...     df2.to_excel(writer, sheet_name='Sheet_name_2')

    To set the library that is used to write the Excel file,
    you can pass the `engine` keyword (the default engine is
    automatically chosen depending on the file extension):

    >>> df1.to_excel('output1.xlsx', engine='xlsxwriter')  # doctest: +SKIP
    '''
    

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。



这篇关于python excel总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程