Python write process change Excel Excel change the read method Python

 

 

 

D:\Python365>pip install properties
Collecting properties
  Downloading properties-0.6.1.tar.gz (52 kB)
     |████████████████████████████████| 52 kB 33 kB/s
Requirement already satisfied: six>=1.7.3 in d:\python365\lib\site-packages (from properties) (1.13.0)
Installing collected packages: properties
    Running setup.py install for properties ... done
Successfully installed properties-0.6.1

D:\Python365>pip install xlwt
Collecting xlwt
  Downloading xlwt-1.3.0-py2.py3-none-any.whl (99 kB)
     |████████████████████████████████| 99 kB 206 kB/s
Installing collected packages: xlwt
Successfully installed xlwt-1.3.0

 

A method of reading and writing changed Excel Python

 

(Note: Part of this article taken from the Internet, because the author is limited, shortcomings, but also hope the message correction.)

       Face blast it.

       Looking back even further.

       And with forward wind, behind a meteor Xu.

       text:

       Python data processing is a major application scenarios, and Excel is the most popular data processing software. Therefore the work related data with Python, and inevitably have to deal with Excel. Standard Excel file (xls / xlsx) has a more complicated format, not as easy as plain text files directly read and write, you need to use third-party libraries to achieve.

The following are on Python3 achieve, if it is Python2, you need to modify the code.

A read Excel table (to xlrd module)

      Xlrd module can only be used to read data operation, data can not be modified.

Copy the code
to xlrd Import               # import modules 
Data xlrd.open_workbook = ( ' movies .xlsx ') # Open file for reading movie .xlsx data.sheets = Table () [0] # is read first (0) Form # , or by the form Gets the name = data.sheet_by_name table (u'Sheet1 ') print (table.nrows) # output table rows print (table.ncols) # output table columns print (table.row_values (0)) # outputs the first line print (table.col_values (0)) # output of the first column Print (table.cell (0,2) .Value) # output elements (0,2) value
Copy the code

Output:

image

Original Excel spreadsheet circumstances:

image

Second, write Excel table (xlwt module)

Copy the code
xlwt Import                             # import modules 
wb = xlwt.Workbook (encoding = ' ascii ') # Create a new Excel (new workbook), the proposal is still with the ascii encoding = wb.add_sheet WS ( ' Weng ') # Create a new form weng ws .write (0, 0, label = ' Hello ') # (0,0) was added ws.write Hello (0,. 1, label = ' World ') # (0,1) was added world ws.write (1 , 0, label = ' Hello ' ) wb.save ( ' weng.xls ') # saved as a file weng.xls
Copy the code

The emergence of this document under the py file path, says:

image

Third, the change Excel table (xlutils module)

Copy the code
to xlrd Import                            # import modules
 from xlutils.copy Import copy # imported copy module xlrd.open_workbook = RB ( ' weng.xls ') # open weng.xls copy file = WB (RB) # copy using copy function ws under xlutils.copy wb.get_sheet = (0) # Gets the ws.write 0 (0, 0, ' changed! ') # change (0,0) value ws.write (8,0, label = ' good ') # increase (8,0) value wb.save ( ' weng.xls ') # save the file
Copy the code

The revised Excel table:

image

Finish here, spare time taking the time to write, and then add it imposed deficiencies.

    Leaves his life, only to return to their roots do?

    Long way to go, but a sword for company.

    Sword, an idea.

 

 

 

 

 

(Note: Part of this article taken from the Internet, because the author is limited, shortcomings, but also hope the message correction.)

       Face blast it.

       Looking back even further.

       And with forward wind, behind a meteor Xu.

       text:

       Python data processing is a major application scenarios, and Excel is the most popular data processing software. Therefore the work related data with Python, and inevitably have to deal with Excel. Standard Excel file (xls / xlsx) has a more complicated format, not as easy as plain text files directly read and write, you need to use third-party libraries to achieve.

The following are on Python3 achieve, if it is Python2, you need to modify the code.

A read Excel table (to xlrd module)

      Xlrd module can only be used to read data operation, data can not be modified.

Copy the code
to xlrd Import               # import modules 
Data xlrd.open_workbook = ( ' movies .xlsx ') # Open file for reading movie .xlsx data.sheets = Table () [0] # is read first (0) Form # , or by the form Gets the name = data.sheet_by_name table (u'Sheet1 ') print (table.nrows) # output table rows print (table.ncols) # output table columns print (table.row_values (0)) # outputs the first line print (table.col_values (0)) # output of the first column Print (table.cell (0,2) .Value) # output elements (0,2) value
Copy the code

Output:

image

Original Excel spreadsheet circumstances:

image

Second, write Excel table (xlwt module)

Copy the code
xlwt Import                             # import modules 
wb = xlwt.Workbook (encoding = ' ascii ') # Create a new Excel (new workbook), the proposal is still with the ascii encoding = wb.add_sheet WS ( ' Weng ') # Create a new form weng ws .write (0, 0, label = ' Hello ') # (0,0) was added ws.write Hello (0,. 1, label = ' World ') # (0,1) was added world ws.write (1 , 0, label = ' Hello ' ) wb.save ( ' weng.xls ') # saved as a file weng.xls
Copy the code

The emergence of this document under the py file path, says:

image

Third, the change Excel table (xlutils module)

Copy the code
to xlrd Import                            # import modules
 from xlutils.copy Import copy # imported copy module xlrd.open_workbook = RB ( ' weng.xls ') # open weng.xls copy file = WB (RB) # copy using copy function ws under xlutils.copy wb.get_sheet = (0) # Gets the ws.write 0 (0, 0, ' changed! ') # change (0,0) value ws.write (8,0, label = ' good ') # increase (8,0) value wb.save ( ' weng.xls ') # save the file
Copy the code

The revised Excel table:

image

Finish here, spare time taking the time to write, and then add it imposed deficiencies.

    Leaves his life, only to return to their roots do?

    Long way to go, but a sword for company.

    Sword, an idea.

 

 

 

 

Guess you like

Origin www.cnblogs.com/xinxihua/p/12616908.html