python用lxml库直接读写office excel文件
来源:互联网 发布:刷弹幕的软件 编辑:程序博客网 时间:2024/06/10 07:37
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.hetongbhdef 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+=1def 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
0 0
- python用lxml库直接读写office excel文件
- 用Python读写Excel文件
- 用Python读写Excel文件
- 用Python读写Excel文件
- 用Python读写Excel文件
- python office 之 读写excel电子表格。
- python读写Excel文件
- python读写excel文件
- Python读写excel文件
- Python 读写Excel文件
- python读写excel文件
- Python读写excel文件
- python读写Excel文件
- Python读写excel文件
- python读写excel文件
- python读写excel文件
- python读写excel文件
- 用Python读写Excel文件总结
- 局部性
- 菜单(menu)简介
- Android2.2、2.3定制修改状态栏布局、背景、去除状态栏按键、添加状态栏按键
- Linux学习记录(18)DNS服务基础
- 强烈推荐转载-Android 性能测试
- python用lxml库直接读写office excel文件
- 黑马程序员--java基础日记--基础语法知识
- 内核对象的两种状态 激活 未激活
- IAR使用过程中问题及解决方法
- HibernateTransactionManager与HibernateTemplater是什么关系,其中一个是否多余?
- PHP接收POST的三种方式
- mysql远程访问
- Android下常见的内存泄露 经典
- 【数据结构】静态查找之顺序查找