首页 > 解决方案 > 使用 openpyxl 编写值和公式?

问题描述

我想知道是否可以使用 openpyxl 或其他方法为单元格编写值和公式。例如,我想存储像“=SUM(A1:A5)”这样的东西以及它的值,比如 100。更具体地说,我想在 XML 文件中填写<f>元素和元素<v>(就像这里描述的那样)。

我猜只使用 openpyxl 是不可能的,因为要读取 Excel 文件,需要指定它是否只是值,并且将值和公式都写入单元格的“值”属性。

最好的方法是什么?可能吗?欢迎任何想法。

作为旁注,我并不特别担心该值是否正确,即与 Excel 的公式计算相匹配。我正在使用 pycel 来评估单元格。

这是一个例子:

from openpyxl import Workbook
import xml.etree.ElementTree as ET
from zipfile import ZipFile

wb = Workbook()
ws = wb.active
ws['A1'], ws['A2'] = 1, 2
ws['A3'] = '=SUM(A1:A2)'
ws['A4'] = '=SWITCH(A1:A2, 1, 2, 2, 1)'

filename = 'testing_xml.xlsx'
wb.save(filename)

with ZipFile(filename, 'r') as zip_obj:
   zip_obj.extractall()
   tree = ET.parse('xl/worksheets/sheet1.xml')

for elem in tree.iter():
    print(elem.tag, elem.attrib, elem.text)

我想知道是否有一种简单的方法可以v为公式单元格A3A4.

{http://schemas.openxmlformats.org/spreadsheetml/2006/main}worksheet {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetPr {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}outlinePr {'summaryBelow': '1', 'summaryRight': '1'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}pageSetUpPr {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}dimension {'ref': 'A1:A4'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetViews {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetView {'workbookViewId': '0'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}selection {'activeCell': 'A1', 'sqref': 'A1'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetFormatPr {'baseColWidth': '8', 'defaultRowHeight': '15'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetData {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}row {'r': '1'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}c {'r': 'A1', 't': 'n'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}v {} 1
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}row {'r': '2'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}c {'r': 'A2', 't': 'n'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}v {} 2
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}row {'r': '3'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}c {'r': 'A3'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}f {} SUM(A1:A2)
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}v {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}row {'r': '4'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}c {'r': 'A4'} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}f {} SWITCH(A1:A2, 1, 2, 2, 1)
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}v {} None
{http://schemas.openxmlformats.org/spreadsheetml/2006/main}pageMargins {'left': '0.75', 'right': '0.75', 'top': '1', 'bottom': '1', 'header': '0.5', 'footer': '0.5'} None

标签: xmlopenpyxlpycel

解决方案


推荐阅读