python用lxml庫直接讀寫office excel檔案
python讀寫excel檔案有好幾個工具。我用過pywin32。但最近發現用直接操縱xml的方法更快,而且不依賴於平臺。excel檔案可以儲存為xml檔案,之後讀寫就變成了操作xml Element。excel 檔案的 格式還有需要了解的地方。下面程式碼是讀寫Cell的例子。
def setCell(table,row,col,value): row=row-1 col=col-1 rows=table.findall("{urn:schemas-microsoft-com:office:spreadsheet}Row") row1=rows[row] cells=row1.findall("{urn:schemas-microsoft-com:office:spreadsheet}Cell") #{urn:schemas-microsoft-com:office:spreadsheet}Index at=0 mycells={} for cell in cells:#MergeAcross #print(cell.attrib) if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}Index")==None: mycells[at]=cell at+=1 else: at=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}Index"]) mycells[at-1]=cell if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross")!=None: at+=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross"]) # print(mycells) # print(col) # input("here") data=mycells.get(col).find("{urn:schemas-microsoft-com:office:spreadsheet}Data") if data==None: data=ET.fromstring('<Data xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" ss:Type="String">CS-2800□</Data>') mycells.get(col).append(data) data.text=value#"合同號:"+contact.hetongbh def setCellWithFont(table,row,col,values): row=row-1 col=col-1 rows=table.findall("{urn:schemas-microsoft-com:office:spreadsheet}Row") row1=rows[row] cells=row1.findall("{urn:schemas-microsoft-com:office:spreadsheet}Cell") #{urn:schemas-microsoft-com:office:spreadsheet}Index at=0 mycells={} for cell in cells:#MergeAcross #print(cell.attrib) if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}Index")==None: mycells[at]=cell at+=1 else: at=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}Index"]) mycells[at-1]=cell if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross")!=None: at+=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross"]) data=mycells.get(col).find("{urn:schemas-microsoft-com:office:spreadsheet}Data") if data==None: data=ET.fromstring('<Data xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" ss:Type="String">CS-2800□</Data>') mycells.get(col).append(data) data.text="".join(values)#"合同號:"+contact.hetongbh else: fonts=data.findall("{http://www.w3.org/TR/REC-html40}Font") at=0 for font in fonts: if at==len(values): break print(at,font.text) font.text=values[at] at+=1 def getCell(table,row,col): row=row-1 col=col-1 rows=table.findall("{urn:schemas-microsoft-com:office:spreadsheet}Row") row1=rows[row] cells=row1.findall("{urn:schemas-microsoft-com:office:spreadsheet}Cell") #{urn:schemas-microsoft-com:office:spreadsheet}Index #print(cells) at=0 mycells={} for cell in cells:#MergeAcross #print(cell.attrib) if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}Index")==None: mycells[at]=cell at+=1 else: at=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}Index"]) mycells[at-1]=cell if cell.attrib.get("{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross")!=None: at+=int(cell.attrib["{urn:schemas-microsoft-com:office:spreadsheet}MergeAcross"]) #print(mycells) data=mycells.get(col).find("{urn:schemas-microsoft-com:office:spreadsheet}Data") if data==None: return ""#None else: #print(data.attrib) if data.attrib.get('{urn:schemas-microsoft-com:office:spreadsheet}Type')=="String": return data.text#"合同號:"+contact.hetongbh elif data.attrib.get('{urn:schemas-microsoft-com:office:spreadsheet}Type')=="Number": return float(data.text)#"合同號:"+contact.hetongbh else: return data.text
相關推薦
python用lxml庫直接讀寫office excel檔案
python讀寫excel檔案有好幾個工具。我用過pywin32。但最近發現用直接操縱xml的方法更快,而且不依賴於平臺。excel檔案可以儲存為xml檔案,之後讀寫就變成了操作xml Element。excel 檔案的 格式還有需要了解的地方。下面程式碼是讀寫Cell的例
Python 外部數據的讀寫(cvs,excel)
文件 pytho 實踐 ticket data 讀取excel sep taf tutorial python 外部數據的讀寫 來自紀路,Python數據科學實踐指南 CSV CSV是什麽? 逗號分隔值(Comma-Separated Values,CSV,有時也稱為字符
使用Python讀寫/追加excel檔案
一:需要用到的庫 xlrd:讀取excel檔案 xlrd:寫入excel檔案 xlutils:修改/追加excel檔案 直接pip install 庫名即可安裝成功 二:官方文件 如果想詳細瞭解,請一定閱讀官方文件 xlrd:http:/
Python讀寫/追加excel檔案Demo
三個工具包 python操作excel的三個工具包如下,注意,只能操作.xls,不能操作.xlsx。 xlrd: 對excel進行讀相關操作 xlwt: 對excel進行寫相關操作 xlutils: 對excel讀寫操作的整合 這三個工具包都可以直接使
【python】pandas庫pd.read_excel操作讀取excel檔案引數整理與例項
除了使用xlrd庫或者xlwt庫進行對excel表格的操作讀與寫,而且pandas庫同樣支援excel的操作;且pandas操作更加簡介方便。 首先是pd.read_excel的引數:函式為: pd.read_excel(io, sheetname=0,he
Python 讀寫操作Excel —— 安裝第三方庫(xlrd、xlwt、xlutils)
保存數據 下載 實用 第三方 直接 install pytho 方法 xls 數據處理是 Python 的一大應用場景,而 Excel 則是最流行的數據處理軟件。因此用 Python 進行數據相關的工作時,難免要和 Excel 打交道。 如果僅僅是要以表單形式保存數據,可
ApolloStudio高手之路(6):用Python以極簡方式讀寫OPC DA、OPC UA資料並實現UI控制元件自動繫結重新整理顯示
OPC(OLE for Process Control, 用於過程控制的OLE)是一個工業標準,OPC是為了連線資料來源(OPC伺服器)和資料的使用者(OPC應用程式)之間的軟體介面標準。資料來源可以是PLC,DCS,條形碼讀取器等控制裝置。隨控制系統構成的不同,作為資料來源的OPC伺服器既可以
用OpenGL的Image特性實現紋理資料的直接讀寫操作
OpenGL的Image特性簡介 Image是在OpenGL 4.2成為core標準的,大概目標是用於通用計算,因此它只能在Compute Shader和Fragment Shader裡使用。它跟一個特定的紋理繫結在一起,所進行的操作會直接影響這個紋理。 紋
python利用lxml讀寫xml格式檔案
之前在轉換資料集格式的時候需要將json轉換到xml檔案,用lxml包進行操作非常方便。 寫xml檔案 a) 用etree和objectify from lxml import etree, objectify E = objectify.Eleme
Linux下直接讀寫物理地址內存
ann erro 分頁 .... gpio 細心 key 單位 開發 虛擬 轉 物理地址 virt_to_phys( *addr );物理 轉 虛擬地址 phys_to_virt( *addr ); 如: unsigned long pProtectVA;
Python常用的文件讀寫操作和字符串操作
dir info load char 編碼 lines resolve values ror 文件讀寫操作 fileUtils.py # -*- coding: utf-8 -*- import os def getFileList(dir, fileList=[]):
使用mysql proxy對數據庫進行讀寫分離
用戶權限 share 數據庫 ava mysql- bit 但是 文件 環境 服務器安排如下: 192.168.100.128 主 192.168.100.129 從 192.168.100.130 mysql-proxy 1、在100.130中下載安裝mysql-pr
[Win32] 直接讀寫磁盤扇區(磁盤絕對讀寫)
ref return rac cpp sig i/o phy 類型 表示 ??本博文由CSDN博主zuishikonghuan所作,版權歸zuishikonghuan全部。轉載請註明出處:http://blog.csdn.net/zuishikonghuan/artic
Python 基礎 - Json文件讀寫
ref csdn spa cnblogs 一個 所有 文本格式 tail pytho JSON介紹 JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。它基於ECMAScript的一個子集。 JSON采用完全獨立於語言的文本格式,但
python之configParser模塊讀寫配置文件
pytho 配置 添加 print name imp 配置文件 所有 con 借鑒:http://www.cnblogs.com/TankXiao/p/3038350.html configParser是python自帶的模塊,用來讀寫配置文件 配置文件的格式:[]包含的叫
python-ConfigParser模塊【讀寫配置文件】
target new start 需要 details string 如何 設置變量 board http://www.codesky.net/article/201003/122500.html http://www.linuxso.com/linuxbiancheng/
Python通過lxml庫遍歷xml通過xpath查詢(標簽,屬性名稱,屬性值,標簽對屬性)
style 去掉 odi 之間 [] 符號 層次結構 div amp xml實例: 版本一: <?xml version="1.0" encoding="UTF-8"?><country name="chain"><provinces>
python通過LXML庫讀取xml命名空間
con family python continue 聲明 heilon style color ont xml實例版本: <a> <city:table xmlns:city="city"> <heilongjia
通過spring抽象路由數據源+MyBatis攔截器實現數據庫自動讀寫分離
註入 兩個 -- 事情 rem 使用註解 connect key值 -m 前言 之前使用的讀寫分離的方案是在mybatis中配置兩個數據源,然後生成兩個不同的SqlSessionTemplate然後手動去識別執行sql語句是操作主庫還是從庫。如下圖所示: 好處是,你
Python之配置文件讀寫
con nbsp 獲取 如果 option 創建 函數 添加 ans ConfigParser模塊 一、創建配置文件 在D盤建立一個配置文件,名字為:test.ini 內容如下: [baseconf] host=127.0.0.1 port=3306 user=root