我曾經有過同樣的問題。您可以解壓縮xlsx並讀取/解釋xml文件。這兩個更重要的文件就是這些。
- XL/pivotCache/pivotCacheDefinition1.xml
- XL/pivotCache/pivotCacheRecords1.xml
第一個,在pivotCacheRecords1.xml原始數據的relationshit,你需要訪問通過索引號,我的意思是,通過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和列的索引與defdict的密鑰相匹配時,標籤是<x>
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).
其餘的是歷史,我希望這會幫助你。
快樂編碼!
正確。但是有一個細節:如果數據透視表數據是隱藏的(只有摘要顯示在表中),它可能不在同一張表中,而在數據對象中。在這種情況下,可以很容易地用例如數據提取xml數據。方舟(解壓xlsx) – 2017-08-10 07:47:30