我曾经遇到过同样的问题。您可以通过解压缩xlsx文件并阅读/解释xml文件来解决。两个最重要的文件是:
- xl/pivotCache/pivotCacheDefinition1.xml
- xl/pivotCache/pivotCacheRecords1.xml
第一个文件包含了pivotCacheRecords1.xml中原始数据的关系,您需要通过索引号访问它。我的意思是,对于pivotCacheRecords1.xml中每个带有<x>
标签的列,您需要通过<x>
标签的索引号获取pivotCacheDefinition1.xml中的数据。为了更好地理解,您需要查看xml文件。
pivotCacheDefinition1.xml
<?xml version="1.0" encoding="UTF-8"?>
<pivotCacheDefinition xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1" refreshedBy="ADNLatam" refreshedDate="42972.64919178241" createdVersion="5" refreshedVersion="6" recordCount="1923161">
<cacheSource type="external" connectionId="1" />
<cacheFields count="26">
<cacheField name="C - Cadenas" numFmtId="0" sqlType="-9">
<sharedItems count="3">
<s v="superA" />
<s v="superB" />
<s v="superC" u="1" />
</sharedItems>
</cacheField>
<cacheField name="C - Locales" numFmtId="0" sqlType="-9"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
<sharedItems count="80">
<s v="Itaugua" />
<s v="Denis Roa" />
<s v="Total" />
<s v="Los Laureles" />
<s v="CDE" />
<s v="S6 Fdo." />
<s v="Central" u="1" />
<s v="Unicompra" u="1" />
<s v="San Lorenzo Centro" u="1" />
</sharedItems>
</cacheField>
</cacheFields>
</pivotCacheDefinition>
</xml>
pivotCacheRecords1.xml
<pivotCacheRecords
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" count="246209">
<r>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<s v="PAÐAL "PAMPERS" BABYSAN REGULAR GDE 9UN"/> #Z - Sku / Descripcion
<s v="07501006720341"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<n v="1"/>
<n v="11990"/>
<n v="2.3199999999999998"/>
<n v="10900"/>
<n v="11990"/>
<n v="1"/>
<d v="2012-02-03T00:00:00"/>
<x v="0"/>
<x v="0"/>
<n v="3"/>
<n v="6"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
<x v="0"/>
</r>
请注意,CacheRecords1标记中的
<x>
是CacheDefinition1标记中
<s>
标记的关系。现在,如果您理解了这一点,就不难创建一个字典,在记录的迭代中使用它。
definitions = '/tmp/scantrack_tmp/xl/pivotCache/pivotCacheDefinition1.xml'
defdict = {}
columnas = []
e = xml.etree.ElementTree.parse(definitions).getroot()
for fields in e.findall('{http://schemas.openxmlformats.org/spreadsheetml/2006/main}cacheFields'):
for cidx, field in enumerate(fields.getchildren()):
columna = field.attrib.get('name')
defdict[cidx] = []
columnas.append(columna)
for value in field.getchildren()[0].getchildren():
tagname = value.tag
defdict[cidx].append(value.attrib.get('v', 0))
我们最终得到了这个字典。
{
0: ['supera', 'superb', u'superc'],
1: ['Terminal',
'CDE',
'Brasilia',
]
3: ['PANTENE', 'DOVE']
...
}
然后你只需要遍历 CacheRecords1 并在标签为
<x>
时,将列的索引与 defdict 中的键进行匹配。
dfdata = []
bdata = '/tmp/scantrack_tmp/xl/pivotCache/pivotCacheRecords1.xml'
for event, elem in xml.etree.ElementTree.iterparse(bdata, events=('start', 'end')):
if elem.tag == '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}r' and event == 'start':
tmpdata = []
for cidx, valueobj in enumerate(elem.getchildren()):
tagname = valueobj.tag
vattrib = valueobj.attrib.get('v')
rdata = vattrib
if tagname == '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}x':
try:
rdata = defdict[cidx][int(vattrib)]
except:
logging.error('this it not should happen index cidx = {} vattrib = {} defaultidcts = {} tmpdata for the time = {} xml raw {}'.format(
cidx, vattrib, defdict, tmpdata,
xml.etree.ElementTree.tostring(elem, encoding='utf8', method='xml')
))
tmpdata.append(rdata)
if tmpdata:
dfdata.append(tmpdata)
elem.clear()
然后你可以将dfdata放入数据框中。
df = pd.DataFrame(dfdata).
其余的就是历史了,我希望这能对你有所帮助。
愉快的编码!