diff --git a/jupytext/header.py b/jupytext/header.py
index b477b5550..c23ff7259 100644
--- a/jupytext/header.py
+++ b/jupytext/header.py
@@ -173,7 +173,7 @@ def header_to_metadata_and_cell(lines, header_prefix, ext=None):
 
         if _JUPYTER_RE.match(line):
             injupyter = True
-        elif not _LEFTSPACE_RE.match(line):
+        elif line and not _LEFTSPACE_RE.match(line):
             injupyter = False
 
         if injupyter: