在python中使用xlrd,xlwt和xlutils从excel文件中删除行 [英] In python removing rows from a excel file using xlrd, xlwt, and xlutils

查看:2526
本文介绍了在python中使用xlrd,xlwt和xlutils从excel文件中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,谢谢.

我有一个python脚本,我在其中打开模板excel文件,添加数据(同时保留样式)并再次保存.我希望能够在保存新的xls文件之前删除未编辑的行.我的模板xls文件具有页脚,因此我想删除页脚之前的多余行.

I have a python script where I am opening a template excel file, adding data (while preserving the style) and saving again. I would like to be able to remove rows that I did not edit before saving out the new xls file. My template xls file has a footer so I want to delete the extra rows before the footer.

这是我加载xls模板的方式:

Here is how I am loading the xls template:

self.inBook = xlrd.open_workbook(file_path, formatting_info=True)
self.outBook = xlutils.copy.copy(self.inBook)
self.outBookCopy = xlutils.copy.copy(self.inBook)

然后我将信息写入outBook,同时从outBookCopy中获取样式并将其应用于我在outbook中修改的每一行.

I then write the info to outBook while grabbing the style from outBookCopy and applying it to each row that I modify in outbook.

那我该如何在outBook删除行之前将其删除呢?谢谢大家!

so how do I delete rows from outBook before writing it? Thanks everyone!

推荐答案

我使用Pandas软件包实现了....

I achieved using Pandas package....

import pandas as pd

#Read from Excel
xl= pd.ExcelFile("test.xls")

#Parsing Excel Sheet to DataFrame
dfs = xl.parse(xl.sheet_names[0])

#Update DataFrame as per requirement
#(Here Removing the row from DataFrame having blank value in "Name" column)

dfs = dfs[dfs['Name'] != '']

#Updating the excel sheet with the updated DataFrame

dfs.to_excel("test.xls",sheet_name='Sheet1',index=False)

这篇关于在python中使用xlrd,xlwt和xlutils从excel文件中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆