Read workbook properties using python and xlrd

大憨熊 提交于 2019-12-22 08:33:57

问题


Is there a way to read the excel file properties using xlrd? I refer not to cell presentation properties, but general workbook properties.

Thanks a lot in advance.


回答1:


Apart from the username (last person to save the worksheet) the Book instance as returned by open_workbook does not seem to have any properties.

I recursively dumped the Book ( dumping its dict if a xlrd.BaseObject) and could not find anything in that way. The test files for sure had an author, company and some custom metadata.

FWIW: LibreOffice does not seem to be able to find author and company either (or does not display them), but it does show custom metadata in the properties.




回答2:


I couldn't find a way to do this with xlrd, but if you only have to read .xlsx files, you can treat them as a Zipfile and read the properties XML file(s). You can see this by changing the .xlsx extension to .zip and opening the file on Windows. An example of reading custom defined properties is below.

from lxml import etree as ET
import zipfile    

def get_custom_properties(filename):
    zip = zipfile.ZipFile(filename)
    props = zip.open('docProps/custom.xml')
    text = props.read()
    xml = ET.fromstring(text)
    # Works on my example document, but I don't know if every 
    # child node will always have exactly one nested node
    return {
        child.attrib['name']: child[0].text
        for child in xml
    }


来源:https://stackoverflow.com/questions/13139949/read-workbook-properties-using-python-and-xlrd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!