温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python 读取有公式cell的结果内容实例方法

发布时间:2020-09-15 00:52:27 来源:脚本之家 阅读:116 作者:paeser 栏目:开发技术

操作Excel通常是用如下三个扩展体:

  1. import xlrd
  2. import xlwt
  3. import openpyxl

wb1 = openpyxl.load_workbook(xlsxFileWithFullPath, data_only=True)

就是加上 “data_only=True” 这个参数。

xlsxFileWithFullPath ---要操作的文件。

加上以后你会发现,还是依然如故,或者是时可时否!

如把文件打开,再保存一遍,执行程序,第一遍可以,第二遍就不行了!

其实, 关于 data_only=True 这个参数有个重要说明:

# data_only (bool) – controls whether cells with formula have either the formula (default) or the value stored the last time Excel read the sheet

这就解释了上述时可时否的问题。

这样解决问题的办法就有了:用程序来完成那个保存文件的任务即可!

(1)

def ReadLine(self, tip1, tip2, movingRC, fixedRC, RorC, totalCells, sheetName, xlsxFileWithFullPath):

[!!]just open and save the file once! why? see bellow!
xlsxDealer.`JustOpenAndSaveTheFile`(xlsxFileWithFullPath)

(2)

def JustOpenAndSaveTheFile(self, file_name):
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(file_name)
xlBook.Save()
xlBook.Close()

问题是解决了,速度就是有点慢!

以上就是本次介绍的全部相关知识点,感谢大家的学习,欢迎转载。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI