如何使用Python实例方法读取含公式的单元格结果内容?
- 内容介绍
- 文章标签
- 相关推荐
本文共计443个文字,预计阅读时间需要2分钟。
操作Excel通常涉及以下三个扩展库:
1. `import xlrd`:用于读取Excel文件。
2.`import xlwt`:用于写入Excel文件。
3.`import openpyxl`:用于处理Excel文件。
示例代码如下:
python
wb1=openpyxl.load_workbook(xlsxFileWithFullPath, data_only=True)其中,`xlsxFileWithFullPath`是要操作的文件路径。添加参数`data_only=True`可以只获取公式计算的结果,而不是公式本身。
操作Excel通常是用如下三个扩展体:
- import xlrd
- import xlwt
- 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()
问题是解决了,速度就是有点慢!
以上就是本次介绍的全部相关知识点,感谢大家的学习,欢迎转载。
本文共计443个文字,预计阅读时间需要2分钟。
操作Excel通常涉及以下三个扩展库:
1. `import xlrd`:用于读取Excel文件。
2.`import xlwt`:用于写入Excel文件。
3.`import openpyxl`:用于处理Excel文件。
示例代码如下:
python
wb1=openpyxl.load_workbook(xlsxFileWithFullPath, data_only=True)其中,`xlsxFileWithFullPath`是要操作的文件路径。添加参数`data_only=True`可以只获取公式计算的结果,而不是公式本身。
操作Excel通常是用如下三个扩展体:
- import xlrd
- import xlwt
- 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()
问题是解决了,速度就是有点慢!
以上就是本次介绍的全部相关知识点,感谢大家的学习,欢迎转载。

