Analysis on the Usage of xlutils Library in python

  • 2021-09-05 00:21:38
  • OfStack

Many small partners believe that, Directly operate excel, Than we use all kinds of code data to process, Direct and simple, It's not so fancy, but in code, processing data, direct software operation is not feasible, we need to use code to deal with it. In fact, the solution to the trouble is very simple. We only need to call professional data processing modules, which can be easily handled. For example, xlutils library in excel processing is introduced and used in detail below.

Brief introduction:

The most common use is replication in excel.

Installation method:


pip install xlutils

Note:

Although excel can be replicated. However, only write operations can be provided, and formats cannot be copied.

Use example:


from xlutils.copy import copy
import xlrd
workBook = xlrd.open_workbook('test.xlsx')
new_workBook = copy(workBook)

Analysis of xlutils in Python

1. Import module


import xlrd
import xlutils.copy

2. Open the module table


book = xlrd.open_workbook('test.xls', formatting_info=True)

3. Copy the module table


wtbook = xlutils.copy.copy(book)
wtsheet = wtbook.get_sheet(0)

4. Write the module table


wtsheet.write(0, 0, 'Ok, changed!')

5. Save the module table


wtbook.save('test.xls')

# When xlrd.open_workbook () is called, if formatting_info=True is not specified, the style of the entire document will be lost after modification. If you do an write operation on 1 cell, you will lose the original style if you do not specify a style.

Notice the method that calls copy (). You can also call copy () directly by declaring from xlutils. copy import copy.


Related articles: