Skip to content

Commit 2845c6d

Browse files
authored
ENH: Add metadata.creation_date and modification_date (py-pdf#1364)
Closes py-pdf#1222
1 parent 85b3e87 commit 2845c6d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

PyPDF2/_reader.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import re
3232
import struct
3333
import zlib
34+
from datetime import datetime
3435
from io import BytesIO
3536
from pathlib import Path
3637
from typing import (
@@ -230,6 +231,48 @@ def producer_raw(self) -> Optional[str]:
230231
"""The "raw" version of producer; can return a ``ByteStringObject``."""
231232
return self.get(DI.PRODUCER)
232233

234+
@property
235+
def creation_date(self) -> Optional[datetime]:
236+
"""
237+
Read-only property accessing the document's **creation date**.
238+
"""
239+
text = self._get_text(DI.CREATION_DATE)
240+
if text is None:
241+
return None
242+
return datetime.strptime(text.replace("'", ""), "D:%Y%m%d%H%M%S%z")
243+
244+
@property
245+
def creation_date_raw(self) -> Optional[str]:
246+
"""
247+
The "raw" version of creation date; can return a ``ByteStringObject``.
248+
249+
Typically in the format D:YYYYMMDDhhmmss[+-]hh'mm where the suffix is the
250+
offset from UTC.
251+
"""
252+
return self.get(DI.CREATION_DATE)
253+
254+
@property
255+
def modification_date(self) -> Optional[datetime]:
256+
"""
257+
Read-only property accessing the document's **modification date**.
258+
259+
The date and time the document was most recently modified.
260+
"""
261+
text = self._get_text(DI.MOD_DATE)
262+
if text is None:
263+
return None
264+
return datetime.strptime(text.replace("'", ""), "D:%Y%m%d%H%M%S%z")
265+
266+
@property
267+
def modification_date_raw(self) -> Optional[str]:
268+
"""
269+
The "raw" version of modification date; can return a ``ByteStringObject``.
270+
271+
Typically in the format D:YYYYMMDDhhmmss[+-]hh'mm where the suffix is the
272+
offset from UTC.
273+
"""
274+
return self.get(DI.MOD_DATE)
275+
233276

234277
class PdfReader:
235278
"""

tests/test_reader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def test_read_metadata(pdf_path, expected):
9595
docinfo.producer_raw
9696
docinfo.subject
9797
docinfo.subject_raw
98+
docinfo.creation_date
99+
docinfo.creation_date_raw
100+
docinfo.modification_date
101+
docinfo.modification_date_raw
98102
if "/Title" in metadict:
99103
assert metadict["/Title"] == docinfo.title
100104

0 commit comments

Comments
 (0)