简体   繁体   English

使用python和xlrd读取工作簿属性

[英]Read workbook properties using python and xlrd

Is there a way to read the excel file properties using xlrd? 有没有办法使用xlrd读取excel文件属性? I refer not to cell presentation properties, but general workbook properties. 我不是指单元格表示属性,而是指一般工作簿属性。

Thanks a lot in advance. 非常感谢提前。

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. 除了用户名(保存工作表的最后一个人)之外,open_workbook返回的Book实例似乎没有任何属性。

I recursively dumped the Book ( dumping its dict if a xlrd.BaseObject) and could not find anything in that way. 我递归地转储了Book(如果是xlrd.BaseObject则转储它的dict )并且无法以这种方式找到任何东西。 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. FWIW:LibreOffice似乎无法找到作者和公司(或不显示它们),但它确实在属性中显示自定义元数据。

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). 我找不到使用xlrd执行此操作的方法,但如果您只需要读取.xlsx文件,则可以将它们视为Zipfile并读取属性XML文件。 You can see this by changing the .xlsx extension to .zip and opening the file on Windows. 您可以通过将.xlsx扩展名更改为.zip并在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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM