python處理Excel文件的幾個模塊

在python中簡單地處理excel文件,有幾個相關的模塊,各有千秋,本文將不定時收錄。html

Python Excel網站收集了關於python處理excel文件的各類信息。python

【注意】使用python處理excel文件前,請多備份文件,以防數據丟失。git

------------------github

0x01 xlrdapi

xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files.app

官方文檔:https://xlrd.readthedocs.io/en/latest/api.html
github項目:https://github.com/python-excel/xlrdide

安裝:網站

pip install xlrd

使用:
只能讀.xls、.xlsx文件(xlrd0.8.0+版本支持讀取xlsx文件)spa

import xlrd
book = xlrd.open_workbook("pcat.xls")
print("The number of worksheets is {0}".format(book.nsheets))
print("Worksheet name(s): {0}".format(book.sheet_names()))
sh = book.sheet_by_index(0)
print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
print("Cell B3 is {0}".format(sh.cell_value(rowx=2, colx=1)))
for rx in range(sh.nrows):
    print(sh.row(rx))

 

0x02 xlwtexcel

xlwt is a library for writing data and formatting information to older Excel files (ie: .xls)

官方文檔:https://xlwt.readthedocs.io/en/latest/api.html
github項目:https://github.com/python-excel/xlwt
安裝:

pip install xlwt

使用:

用xlwt建立一個簡單的.xls文件

import xlwt
from datetime import datetime

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
    num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='YYYY-MM-DD HH:MM:SS')

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

 

0x03 xlutils

This package provides a collection of utilities for working with Excel files.
官方文檔:https://xlutils.readthedocs.io/en/latest/api.html
github項目:https://github.com/python-excel/xlutils

安裝:
(若是沒安裝xlrd、xlwt,會自動安裝這2個模塊)

pip install xlutils

使用:

import xlrd
import xlwt
import xlutils
import xlutils.copy as copy

rdbook = xlrd.open_workbook('first.xls')
wtbook = copy.copy(rdbook)
wtsheet = wtbook.get_sheet(0)
type(wtsheet)
wtsheet.write(0,0,'pcat.cc')
wtbook.save('second.xls')

 

0x04 openpyxl

A Python library to read/write Excel 2010 xlsx/xlsm files.
官方文檔:https://openpyxl.readthedocs.io/en/stable/
安裝:

pip install openpyxl

使用:
寫xlsx文件

from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("sample.xlsx")

讀xlsx文件

from openpyxl import load_workbook
wb = load_workbook(filename='pcat.xlsx')
sheet_ranges = wb['Sheet1']
print(sheet_ranges['A2'].value)

注意:
openpyxl不支持.xls格式。
讀寫文件前記得多備註,有時候可能有bug。

 

0x05 XlsxWriter

XlsxWriter is a Python module for creating Excel XLSX files.
官方文檔: https://xlsxwriter.readthedocs.io/
github項目:https://github.com/jmcnamara/XlsxWriter
安裝:

pip install xlsxwriter

使用:

import xlsxwriter
workbook = xlsxwriter.Workbook('hello_world.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()

注意:XlsxWriter不支持.xls格式。

相關文章
相關標籤/搜索