主頁 > 後端開發 > python的pylightxl模塊

python的pylightxl模塊

2023-05-18 07:36:09 後端開發

pylightxl 比較小,沒有依賴,對python2、3都支持

  • https://pylightxl.readthedocs.io/en/latest/index.html pylightxl 的檔案,可以自己去查看
  • 支持的檔案后綴:.xlsx, .xlsm 和.csv
* 不支持.xls檔案(Microsoft Excel 2003 和更早的檔案)
* 不支持單元格資料以外的任何內容(不支持圖形、影像、宏、格式)
* 不支持超過 536,870,912 個單元格的作業表單元格資料(32 位串列限制),如果需要更多資料存盤,請使用 64 位,
  • 安裝
    pip install pylightxl

  • 讀取excel檔案

import pylightxl as xl
strExcelFilePath = "C:\\Users\\Administrator\\Desktop\\LogTestFile\\1111.xlsx"
db = xl.readxl(strExcelFilePath)
#當然了有更多的方式去讀取
import pylightxl as xl
strExcelFilePath = "C:\\Users\\Administrator\\Desktop\\LogTestFile\\1111.xlsx"
db = xl.readxl(strExcelFilePath , ws="Sheet1")#這里的ws是excel里面的作業簿

當excel檔案里面的內容為:

db.ws(ws='Sheet1').address(address='A1')
db.ws(ws='Sheet1').index(row=1,col=1)
#這兩句代表的意思都是一樣的,是去讀取A1位置上的資料是多少
#如果A1這個位置上沒有資料,就會回傳一個空,回傳的值:**''**

執行結果:

db.ws(ws='Sheet1').address(address='C1', output='f')#如果單元格有公式就輸出公式
db.ws(ws='Sheet1').address(address='C2', output='c')#如果有注釋就輸出注釋

db.ws(ws='Sheet1').set_emptycell(val=0)#將所有沒有資料的單元格都寫上0

db.ws(ws='Sheet1').range(address='A1:C2')#指定一個范圍進行輸出,回傳值是一個串列
db.ws(ws='Sheet1').range(address='A1:B1', output='f')#指定一個范圍查看公式,沒有公式的單元格會回傳一個"=",這個回傳也是串列哦
db.ws(ws='Sheet1').row(row=1)#獲取整行的資料,回傳串列
db.ws(ws='Sheet1').col(col=1)#獲取整列的資料,回傳串列
  • 更新資料,也可以叫寫入資料
db.ws(ws='Sheet1').update_range(address='A1:B1', val=10)#更新資料,也可以叫寫入資料
db.ws(ws='Sheet1').update_address(address='A1', val=100)#也可以這樣寫入,也可以叫寫入資料
db.ws(ws='Sheet1').update_index(row=1, col=1, val=10)#還可以使用這樣的方式寫入資料
db.ws(ws='Sheet1').update_address(address='C1', val='=B1+100')#更新單元格公式,寫入公式
db.ws(ws='Sheet1').update_index(row=1, col=3, val='=B1+100')#更新單元格公式,寫入公式
  • 指定一個區域,只對這個區域做操作
db.add_nr(name='name1', ws='Sheet1', address='A1:B2')  #name 可以隨便寫,ws是作業簿的名字,address是范圍,這里不能用index(row 、col)

db.nr(name='name1')#獲取定義這個范圍里面的內容

db.nr_loc(name='name1')#查看定義這個范圍是那些

db.update_nr(name='name1', val=10)#更新一個范圍內的值

db.nr_names #查看定義了多少個范圍

db.remove_nr(name='name1')#洗掉一個范圍
  • 保存,有沒有發現上面的陳述句更新了資料,但是我們打開excel檔案還是之前的東西,那是因為沒有進行寫入
    xl.writexl(db=db, fn='你要保存的excel檔案位置,可以是之前的excel檔案,也可以是一個新的excel檔案')#寫入資料
import pylightxl as xl

# take this list for example as our input data that we want to put in column A
mydata = https://www.cnblogs.com/zytlk/p/[10,20,30,40]

# create a blank db
db = xl.Database()

# add a blank worksheet to the db
db.add_ws(ws="Sheet1")

# loop to add our data to the worksheet
for row_id, data in enumerate(mydata, start=1):
    db.ws(ws="Sheet1").update_index(row=row_id, col=1, val=data)

# write out the db
xl.writexl(db=db, fn=strExcelFilePath2)

API

3.1. readxl
pylightxl.pylightxl.readxl(fn, ws=None)
Reads an xlsx or xlsm file and returns a pylightxl database

Parameters:	
fn (Union[str, pathlib.Path]) – Excel file path, also supports Pathlib.Path object, as well as file-like object from with/open
ws (Union[str,List[str]], optional) – sheetnames to read into the database, if not specified - all sheets are read entry support single ws name (ex: ws=’sh1’) or multi (ex: ws=[‘sh1’, ‘sh2’]), defaults to None
Returns:	
pylightxl Database

Return type:	
Database
3.2. writexl
pylightxl.pylightxl.writexl(db, fn)
Writes an excel file from pylightxl.Database

Parameters:	
db (Database) – database contains sheetnames, and their data
fn (Union[str, pathlib.path]) – file output path

3.3.1. Database Class
classpylightxl.pylightxl.Database
add_nr(name, ws, address)
Add a NamedRange to the database. There can not be duplicate name or addresses. A named range that overlaps either the name or address will overwrite the database’s existing NamedRange

Parameters:	
name (str) – NamedRange name
ws (str) – worksheet name
address (str) – range of address (single cell ex: “A1”, range ex: “A1:B4”)
add_ws(ws, data=https://www.cnblogs.com/zytlk/p/None)
Logs worksheet name and its data in the database

Parameters:	
ws (str) – worksheet name
data (dict, optional) – dictionary of worksheet cell values (ex: {‘A1’: {‘v’:10,’f’:’’,’s’:’’, ‘c’: ‘’}, ‘A2’: {‘v’:20,’f’:’’,’s’:’’, ‘c’: ‘’}}), defaults to None
nr(name, formula=False, output='v')
Returns the contents of a name range in a nest list form [row][col]

Parameters:	
name (str) – NamedRange name
formula (bool, optional) – flag to return the formula of this cell, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
nest list form [row][col]

Return type:	
List[list]

nr_loc(name)
Returns the worksheet and address loction of a named range

Parameters:	name (str) – NamedRange name
Returns:	[worksheet, address]
Return type:	List[str]
nr_names
Returns the dictionary of named ranges ex: {unique_name: unique_address, …}

Returns:	{unique_name: unique_address, …}
Return type:	Dict[str, str]
remove_nr(name)
Removes a Named Range from the database

Parameters:	name (str) – NamedRange name
remove_ws(ws)
Removes a worksheet and its data from the database

Parameters:	ws (str) – worksheet name
rename_ws(old, new)
Renames an existing worksheet. Caution, renaming to an existing new worksheet name will overwrite

Parameters:	
old (str) – old name
new (str) – new name
set_emptycell(val)
Custom definition for how pylightxl returns an empty cell

Parameters:	val (Union[str,int,float]) – (default=’’) empty cell value
update_nr(name, val)
Updates a NamedRange with a single value. Raises UserWarning if name not in workbook.

Parameters:	
name (str) – NamedRange name
val (Union[int,float,str]) – cell value; equations are string and must being with “=”
ws(ws)
Indexes worksheets within the database

Parameters:	ws (str) – worksheet name
Returns:	pylightxl.Database.Worksheet class object
Return type:	Worksheet
ws_names
Returns a list of database stored worksheet names

Returns:	list of worksheet names
Return type:	List[str]
3.3.2. Worksheet Class
classpylightxl.pylightxl.Worksheet(data=https://www.cnblogs.com/zytlk/p/None)
address(address, formula=False, output='v')
Takes an excel address and returns the worksheet stored value

Parameters:	
address (str) – Excel address (ex: “A1”)
formula (bool, optional) – flag to return the formula of this cell, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
cell value

Return type:	
Union[int, float, str, bool]

col(col, formula=False, output='v')
Takes a col index input and returns a list of cell data

Parameters:	
col (int) – col index (start at 1 that corresponds to column “A”)
formula (bool, optional) – flag to return the formula of this cell, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
list of cell data

Return type:	
List[Union[int, float, str, bool]]

cols
Returns a list of cols that can be iterated through

Returns:	list of cols-lists (ex: [[11,21],[12,22],[13,23]] for 2 rows with 3 columns of data
Return type:	Iterable[List[Union[int, float, str, bool]]]
index(row, col, formula=False, output='v')
Takes an excel row and col starting at index 1 and returns the worksheet stored value

Parameters:	
row (int) – row index (starting at 1)
col (int) – col index (start at 1 that corresponds to column “A”)
formula (bool, optional) – flag to return the formula of this cell, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
cell value

Return type:	
Union[int, float, str, bool]

keycol(key, keyindex=1)
Takes a column key value (value of any cell within keyindex row) and returns the entire column, no match returns an empty list

Parameters:	
key (Union[str,int,float,bool]) – any cell value within keyindex row (type sensitive)
keyindex (int, optional) – option keyrow override. Must be >0 and smaller than worksheet size, defaults to 1
Returns:	
list of the entire matched key column data (only first match is returned)

Return type:	
List[Union[str,int,float,bool]]

keyrow(key, keyindex=1)
Takes a row key value (value of any cell within keyindex col) and returns the entire row, no match returns an empty list

Parameters:	
key (Union[str,int,float,bool]) – any cell value within keyindex col (type sensitive)
keyindex (int, optional) – option keyrow override. Must be >0 and smaller than worksheet size, defaults to 1
Returns:	
list of the entire matched key row data (only first match is returned)

Return type:	
List[Union[str,int,float,bool]]

range(address, formula=False, output='v')
Takes a range (ex: “A1:A2”) and returns a nested list [row][col]

Parameters:	
address (str) – cell range (ex: “A1:A2”, or “A1”)
formula (bool, optional) – returns the values if false, or formulas if true of cells, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
nested list [row][col] regardless if range is a single cell or a range

Return type:	
_type_

row(row, formula=False, output='v')
Takes a row index input and returns a list of cell data

Parameters:	
row (int) – row index (starting at 1)
formula (bool, optional) – flag to return the formula of this cell, defaults to False
output (str, optional) – output request “v” for value, “f” for formula, “c” for comment, defaults to ‘v’
Returns:	
list of cell data

Return type:	
List[Union[int, float, str, bool]]

rows
Returns a list of rows that can be iterated through

Returns:	list of rows-lists (ex: [[11,12,13],[21,22,23]] for 2 rows with 3 columns of data
Return type:	Iterable[List[Union[int, float, str, bool]]]
set_emptycell(val)
Custom definition for how pylightxl returns an empty cell

Parameters:	val (Union[int, float, str]) – (default=’’) empty cell value
size
Returns the size of the worksheet (row/col)

Returns:	list of [maxrow, maxcol]
Return type:	List[int]
ssd(keyrows='KEYROWS', keycols='KEYCOLS')
Runs through the worksheet and looks for “KEYROWS” and “KEYCOLS” flags in each cell to identify the start of a semi-structured data. A data table is read until an empty header is found by row or column. The search supports multiple tables.

Parameters:	
keyrows (str, optional) – a flag to indicate the start of keyrow’s cells below are read until an empty cell is reached, defaults to ‘KEYROWS’
keycols (str, optional) – a flag to indicate the start of keycol’s cells to the right are read until an empty cell is reached, defaults to ‘KEYCOLS’
Returns:	
list of data dict in the form of [{‘keyrows’: [], ‘keycols’: [], ‘data’: [[], …]}, {…},]

Return type:	
List[Dict[str,list]]

update_address(address, val)
Update worksheet data via address

Parameters:	
address (str) – excel address (ex: “A1”)
val (Union[int, float, str, bool]) – cell value; equations are strings and must begin with “=”
update_index(row, col, val)
Update worksheet data via index

Parameters:	
row (int) – row index
col (int) – column index
val (Union[int, float, str, bool]) – cell value; equations are strings and must begin with “=”
update_range(address, val)
Update worksheet data via address range with a single value

Parameters:	
address (str) – excel address (ex: “A1:B3”)
val (Union[int, float, str, bool]) – cell value; equations are strings and must begin with “=”
3.3.3. Support Functions
pylightxl.pylightxl.utility_address2index(address)
Convert excel address to row/col index

Parameters:	address (str) – Excel address (ex: “A1”)
Returns:	list of [row, col]
Return type:	List[int]
pylightxl.pylightxl.utility_index2address(row, col)
Converts index row/col to excel address

Parameters:	
row (int) – row index (starting at 1)
col (int) – col index (start at 1 that corresponds to column “A”)
Returns:	
str excel address

Return type:	
str

pylightxl.pylightxl.utility_columnletter2num(text)
Takes excel column header string and returns the equivalent column count

Parameters:	text (str) – excel column (ex: ‘AAA’ will return 703)
Returns:	int of column count
Return type:	int
pylightxl.pylightxl.utility_num2columnletters(num)
Takes a column number and converts it to the equivalent excel column letters

Parameters:	num (int) – column number
Returns:	excel column letters
Return type:	str

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/552657.html

標籤:Python

上一篇:Spring Cloud開發實踐(五): Consul - 服務注冊的另一個選擇

下一篇:返回列表

標籤雲
其他(159165) Python(38144) JavaScript(25431) Java(18055) C(15228) 區塊鏈(8267) C#(7972) AI(7469) 爪哇(7425) MySQL(7191) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5871) 数组(5741) R(5409) Linux(5340) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4572) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2433) ASP.NET(2403) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) .NET技术(1975) 功能(1967) Web開發(1951) HtmlCss(1938) python-3.x(1918) C++(1917) 弹簧靴(1913) xml(1889) PostgreSQL(1878) .NETCore(1861) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • python的pylightxl模塊

    pylightxl 比較小,沒有依賴,對python2、3都支持 https://pylightxl.readthedocs.io/en/latest/index.html pylightxl 的檔案,可以自己去查看 支持的檔案后綴:.xlsx, .xlsm 和.csv * 不支持.xls檔案(Mi ......

    uj5u.com 2023-05-18 07:36:09 more
  • Spring Cloud開發實踐(五): Consul - 服務注冊的另一個選擇

    Consul 是微服務網路解決方案之一, 用于管理跨網路和多云環境服務之間的安全網路連接, 提供服務發現, 服務網格, 流量管理和自動更新. 可以單獨部署, 也可以分布式部署. Consul 內建安全通信選項, 使用 Go 語言撰寫, 啟動資源消耗小, 腳本化配置, 對容器部署方式更友好 ......

    uj5u.com 2023-05-18 07:35:46 more
  • 我的第二次博客作業

    AZ-1的第二次博客作業 AZ-1 - 博客園 (cnblogs.com) 前言 題量 題目集4題量較小。 題目集5題量較小。 期中考試題量中等。 難度 題目集4雖然只有一道題,但是題目很長,難度很大,讓人在看到的時就心生畏懼。選單4在選單3的基礎上增加了大量的錯誤輸入,大大增加了程式的代碼量,很考 ......

    uj5u.com 2023-05-18 07:35:09 more
  • SpringBoot+MyBatis+MySQL電腦商城專案實戰(四)用戶注冊—控制層

    5 注冊-控制層 5.1 創建回應 狀態碼、狀態碼描述資訊、資料。這部分功能封裝到一個類中,將這類作為方法回傳值,回傳給前端瀏覽器。 package com.cy.store.util; import java.io.Serializable; /** * Json格式的資料進行回應 */ publ ......

    uj5u.com 2023-05-18 07:34:39 more
  • ThreadLocal 的原理講述 + 基于ThreadLocal實作MVC中的M層的事務

    ThreadLocal 的原理講述 + 基于ThreadLocal實作MVC中的M層的事務控制 每博一文案 生活不是努力了就可以變好的,喜歡做的事情也不是輕易就可以做的。以前總聽別人說, 堅持就好了,努力就好了,都會好的,可是真的做起來壓根就不是這樣。這種時候要怎么辦? 這種時候還能輕易地相信時間嗎 ......

    uj5u.com 2023-05-18 07:33:34 more
  • 認識Java

    Java的產生和發展 產生與發展歷程 1991年,由Sun公司開發Oak,最初為家用消費電子產品進行編程,是Java前身。 1994年,使用Oak語言撰寫了Web瀏覽器 1995年,改名為Java,96年發布JDK1.1 … 1998年,發布JDK1.2,從語言發展為平臺 … 2004年,發布JDK ......

    uj5u.com 2023-05-18 07:32:52 more
  • Java中列印物件輸出的字串到底是什么

    列印輸出的Java物件是一知半解的字串,那么這個字串是怎么來的?代表什么?我們如何列印出物件中的資料呢? ......

    uj5u.com 2023-05-18 07:32:47 more
  • MVC 三層架構案例詳細講解

    MVC 三層架構案例詳細講解 @ 每博一文案 多讀書,書中有,你對生活,困難所解不開的答案 比如:《殺死一只是更鳥》中提到的 對應我們:我們努力中考,高考,升本,考研,每天都在努力學習,但是某天突然想到萬一沒有考上的話,那現在的努力又有什么意義呢? 答案:在《殺死一只是更鳥》里有這樣一段話: > 勇 ......

    uj5u.com 2023-05-18 07:25:57 more
  • Java IO流 flush()的作用和緩沖流

    Java 緩沖流和flush()的作用 哪些流是緩沖流,哪些流帶有緩沖區? 根據Java官方檔案關于Buffered Streams的介紹,緩沖流有四種: BufferedInputStream:包裝位元組輸入流 BufferedOutputStream:包裝位元組輸出流 BufferedReader: ......

    uj5u.com 2023-05-16 19:20:20 more
  • java 獲取ip

    獲取ip資訊 public static String getRealIp(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == ......

    uj5u.com 2023-05-16 19:20:16 more