python xlwings

Downloads: pywin32com need to install and comtypes

Download :( used to download three packages)

https://pypi.org/project/

In the official website can be downloaded to search

Install them under Scripts

Be sure to install pywin32com and comtypes, the final installation xlwings


import xlwings as xls
newbook = xls.Book()  # this will create a new workbook

It opens a following, nothing else

 wb = xls.Book(r"D:\中雅兼职资料\待办总.xlsx")  # connect to an existing file in the current working directory

The show will open xlsx file, open it only

sht = wb.sheets['待办1号']

Read the corresponding worksheet

sht.range('A50').value = 'Foo 1'
设置单元格值(并未自动保存)
sht.range('A50').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]

Extension: 50 line fill [ 'Foo 1', 'Foo 2', 'Foo 3']

Line fill 51 [10.0, 20.0, 30.0]

>>> sht.range('A1').value = [[1],[2],[3],[4],[5]]  # Column orientation (nested list)
>>> sht.range('A1:A5').value
[1.0, 2.0, 3.0, 4.0, 5.0]
>>> sht.range('A1').value = [1, 2, 3, 4, 5]
>>> sht.range('A1:E1').value
[1.0, 2.0, 3.0, 4.0, 5.0]

Each sub-list of lists, have expressed a row

To write a list in column orientation to Excel, use transposesht.range('A1').options(transpose=True).value = [1,2,3,4]

Equivalent [[1], [2], [3], [4], [5]] Results: >> sht.range ( 'A1: A5 ') value [1.0, 2.0, 3.0, 4.0, 5.0].

region = sht.range('A1:C3').value

List List assignment region

Regional Development:

 sht.range('A1').expand().value

It will represent all the cells connected to the A1 and continuous

There are a number of functional cooperation with other package, see https://docs.xlwings.org/zh_CN/latest/quickstart.html

2d lists: If the row or column orientation has to be preserved, set ndim in the Range options. This will return the Ranges as nested lists (“2d lists”):

>>> sht.range('A1:A5').options(ndim=2).value
[[1.0], [2.0], [3.0], [4.0], [5.0]]
>>> sht.range('A1:E1').options(ndim=2).value
[[1.0, 2.0, 3.0, 4.0, 5.0]]

How presented in a more intuitive reading the value of time? Order options (ndim = 2)

 

Published 19 original articles · won praise 0 · Views 797

Guess you like

Origin blog.csdn.net/weixin_44151772/article/details/104330220