python handling Excel files several modules

Simply process in python excel document, there are several related modules, is different, the timing will not be included herein.

Python Excel website to collect information on a variety of processing python excel file.

[Note] before treatment using python excel files, backup files like him to prevent data loss.

------------------

0x01 xlrd

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

The official document: https://xlrd.readthedocs.io/en/latest/api.html
GitHub project: https://github.com/python-excel/xlrd

installation:

pip install xlrd

Use:
can only read .xls, .xlsx file (xlrd0.8.0 + version supports reading xlsx file)

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 xlwt

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

The official document: https://xlwt.readthedocs.io/en/latest/api.html
GitHub project: https://github.com/python-excel/xlwt
installation:

pip install xlwt

use:

Create a simple .xls file xlwt

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.
Official document: https://xlutils.readthedocs.io/en/latest/api.html
GitHub project: https://github.com/python-excel/xlutils

Installation:
(If you do not install xlrd, xlwt, will automatically install this two modules)

pip install xlutils

use:

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.
Official document: https://openpyxl.readthedocs.io/en/stable/
installation:

pip install openpyxl

Use:
write xlsx file

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")

Read xlsx file

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

Note:
openpyxl .xls format is not supported.
Remember to read and write files before notes, sometimes there may be bug.

 

0x05 XlsxWriter

XlsxWriter is a Python module for creating Excel XLSX files.
Official document: https://xlsxwriter.readthedocs.io/
GitHub project: https://github.com/jmcnamara/XlsxWriter
installation:

pip install xlsxwriter

use:

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

Note:
XlsxWriter .xls format is not supported.

 

Guess you like

Origin www.cnblogs.com/pcat/p/11628317.html