使用Python 2获取Excel文件属性(作者、最后保存/打印时间)
Hey there! The os.path module only gives you filesystem-level metadata (like access or modify time)—that’s not the same as the document-specific properties stored inside the Excel file itself. To get those details like author, last save time, or last print time, you’ll need a library that can parse the Excel file’s internal structure. Here’s how to do it for both common Excel formats:
For .xls Files (Excel 97-2003)
Use the xlrd library (version 1.2.0 is the last release that supports Python 2). First, install it via pip:
pip install xlrd==1.2.0
Then use this code to extract the properties:
import xlrd file_path = "your_file.xls" workbook = xlrd.open_workbook(file_path) # Get document properties props = workbook.doc_properties print("Author:", props.author) print("Last saved time:", props.last_saved_date) print("Last printed time:", props.last_printed_date)
Note: last_printed_date might return None if the file was never printed, or if the property wasn’t saved by the Excel version used.
For .xlsx Files (Excel 2007+)
Use openpyxl (version 2.6.4 is the last release compatible with Python 2). Install it with:
pip install openpyxl==2.6.4
Then extract the properties like this:
from openpyxl import load_workbook file_path = "your_file.xlsx" workbook = load_workbook(file_path, read_only=True) # Access core properties core_props = workbook.properties print("Author:", core_props.creator) print("Last saved time:", core_props.modified) # Access extended properties (for last print time) ext_props = workbook.extended_properties print("Last printed time:", ext_props.get('lastPrinted'))
Again, lastPrinted might be None if the property isn’t set in the file.
Important Notes
- Make sure to install the exact versions specified—newer releases of these libraries dropped support for Python 2 entirely.
- Some properties might be missing depending on how the Excel file was saved (e.g., if the user disabled saving metadata in Excel settings).
内容的提问来源于stack exchange,提问作者András P.

