Plug in wings and let Excel fly--xlwings (iii)

Source: Internet
Author: User

    1. Xlwings Basic Objects

      Xlwings Basic Objects

The app is equivalent to an Excel program, book equivalent to a workbook. n Excel programs are represented by apps, and N workbooks are represented by books.

    1. Actions on a workbook
      #导入xlwings模块import xlwings as xw#新建工作簿,这里默认为新建了一个App,即打开Excel程序,并新建一个工作簿xw.Book()#引用工作簿xw.Book(‘工作簿3‘)# 引用工作簿也可以用booksxw.books[‘工作簿3’]#打开已有工作簿xw.Book(r‘e:\myworkbook.xlsx‘)
    2. Operations on sheet and range
      import xlwings as xwwb=xw.Book()# 引用名字为"sheet2"的sheetsht=wb.sheets[‘sheet2‘]# 引用"sheet2"中的B1单元格rng=sht.range(‘B1‘)# 将B1单元格的值改为666rng.value=666
Data
  1. One-dimensional data
    A list of Python that can be exchanged with columns in Excel, a one-dimensional list in Python, and a row of data by default in Excel
    import xlwings as xwsht=xw.sheets.active# 将1,2,3分别写入了A1,B1,C1单元格中sht.range(‘A1‘).value=[1,2,3]# 将A1,B1,C1单元格的值存入list1列表中list1=sht.range(‘A1:C1‘).value# 将1,2,3分别写入了A1,A2,A3单元格中sht.range(‘A1‘).options(transpose=True).value=[1,2,3]# 将A1,A2,A3单元格中值存入list1列表中list1=sht.range(‘A1:A3‘).value
  2. Two-dimensional data
    A two-dimensional list of Python that can be converted to columns in Excel. A two-dimensional list, that is, an element in a list or a list. In Excel, a list element in a two-dimensional list that represents a column in an Excel table. For example:
    # 将a1,a2,a3输入第一列,b1,b2,b3输入第二列list1=[[‘a1’,‘a2‘,‘a3‘],[‘b1‘,‘b2‘,‘b3‘]]sht.range(‘A1‘).value=list1

    Run results
    # 将A1:B3的值赋给二维列表list1list1=sht.range(‘A1:B3‘).value
  3. Selection table for regions in Excel
    # 选取第一列rng=sht. range(‘A1‘).expand(‘down‘)rng.value=[‘a1‘,‘a2‘,‘a3‘]

    First column
    # 选取第一行rng=sht.range(‘A1‘).expand(‘right‘)rng=[‘a1‘,‘b1‘]

    First line
    # 选取表格rng.sht.range(‘A1‘).expand(‘table‘)rng.value=[[‘a1’,‘a2‘,‘a3‘],[‘b1‘,‘b2‘,‘b3‘]]

    Form
End
    • This article begins with a brief introduction to the operation of Excel workbooks, worksheets, and cells using the Xlwings module in Python.
    • Xlwings, how to write and reference data to the rows and columns of an Excel table
    • Writing or referencing the data of an Excel worksheet through a two-dimensional or one-dimensional list simplifies the code and improves the efficiency of the program. Because Python reads and writes Excel tables faster than the Python program itself.



Luckyfrog
Links: Http://www.jianshu.com/p/de7efe591c12
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Plug in wings and let Excel fly--xlwings (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.