title: How to replace text in a PDF using Python?
body: <p>I have taken the code from another thread <a href="https://stackoverflow.com/a/57665694/13972092">here</a> that uses the library PyPDF2 to parse and replace the text of a PDF. The given example PDF in the thread is parsed as a <code>PyPDF2.generic.DecodedStreamObject</code>. I am currently working with a PDF that the company has provided me that was created using Microsoft Word's Export to PDF feature. This generates a <code>PyPDF2.generic.EncodedStreamObject</code>. From exploration, the main difference is that there is what appears to be kerning in some places in the text.</p>
  <p>This caused two problems for me with the sample code. Firstly, the line <code>if len(contents) &gt; 0:</code> in <code>main</code> seems to get erroneously triggered and attempts to use the key of the EncodedStreamObject dictionary instead of the EncodedStreamObject itself. To work around this, I commented out the if block and used the code in the else block for both cases.</p>
  <p>The second problem was that the (what I assume are) kerning markings broke up the text I was trying to replace. I noticed that kerning was not in every line, so I made the assumption that the kerning markers were not strictly necessary, and tried to see what the output would look like with them removed. The text was structured something like so: [(Thi)4(s)-1(is t)2(ext)]. I replaced the line in the sample code <code>replaced_line = line</code> in <code>replace_text</code> with <code>replaced_line = &quot;[(&quot; + &quot;&quot;.join(re.findall(r'\((.*?)\)', line)) + &quot;)] TJ&quot;</code>. This preserved the observed structure while allowing the text to be searched for replacements. I verified this was actually replacing the text of the line.</p>
  <p>Neither of those changes prevented the code from executing, however the output PDF seems to be <strong>completely unchanged</strong> despite the code <em>appearing</em> to work using print statements to check if the replaced line has the new text. I initially assumed this was because of the if block in process_data that determined if it was Encoded or Decoded. However, I dug through the actual source code for this library <a href="https://github.com/mstamy2/PyPDF2/blob/master/PyPDF2/generic.py" rel="nofollow noreferrer">located here</a>, and it seems that if the object is Encoded, it generates a Decoded version of itself which the if block reflects. My only other idea is that the if block that I commented out in <code>main</code> wasn't erroneously catching my scenario, but was instead handling it incorrectly. I have no idea how I would fix it so that it handles it properly.</p>
  <p>I feel like I'm incredibly close to solving this, but I'm at my wits end as to what to do from here. I would ask the poster of the linked solution in a comment, but I do not have enough reputation to comment on SO. Does anyone have any leads on how to solve this problem? I don't particularly care what library or file format is used, but it must retain the formatting of the Word document I have been provided. I have already tried exporting to HTML, but that removes most of the formatting and also the header. I have also tried converting the .docx to PDF in Python, but that requires me to actually have Word installed on the machine, which is not a cross-platform solution. I also explored using RTF, but from what I found the solution for that file type is to convert it to a .docx and then to PDF.</p>
  <p>Here is the full code that I have so far:</p>
  <pre><code>
  import PyPDF2
  import re
  
  
  def replace_text(content, replacements=dict()):
      lines = content.splitlines()
  
      result = &quot;&quot;
      in_text = False
  
      for line in lines:
          if line == &quot;BT&quot;:
              in_text = True
  
          elif line == &quot;ET&quot;:
              in_text = False
  
          elif in_text:
              cmd = line[-2:]
              if cmd.lower() == 'tj':
                  replaced_line = &quot;[(&quot; + &quot;&quot;.join(re.findall(r'\((.*?)\)', line)) + &quot;)] TJ&quot;
                  for k, v in replacements.items():
                      replaced_line = replaced_line.replace(k, v)
                  result += replaced_line + &quot;\n&quot;
              else:
                  result += line + &quot;\n&quot;
              continue
  
          result += line + &quot;\n&quot;
  
      return result
  
  
  def process_data(obj, replacements):
      data = obj.getData()
      decoded_data = data.decode('utf-8')
  
      replaced_data = replace_text(decoded_data, replacements)
  
      encoded_data = replaced_data.encode('utf-8')
      if obj.decodedSelf is not None:
          obj.decodedSelf.setData(encoded_data)
      else:
          obj.setData(encoded_data)
  
  
  pdf = PyPDF2.PdfFileReader(&quot;template.pdf&quot;)
  # pdf = PyPDF2.PdfFileReader(&quot;sample.pdf&quot;)
  writer = PyPDF2.PdfFileWriter()
  
  replacements = {
      &quot;some text&quot;: &quot;replacement text&quot;
  }
  
  for page in pdf.pages:
  
      contents = page.getContents()
  
      # if len(contents) &gt; 0:
      #   for obj in contents:
      #       streamObj = obj.getObject()
      #       process_data(streamObj, replacements)
      # else:
      process_data(contents, replacements)
  
      writer.addPage(page)
  
  with open(&quot;output.pdf&quot;, 'wb') as out_file:
      writer.write(out_file)
  </code></pre>
  <p>EDIT:</p>
  <p>I've somewhat tracked down the source of my problems. The line <code>obj.decodedSelf.setData(encoded_data)</code> seems to not actually set the data properly. After that line, I added</p>
  <pre><code>print(encoded_data[:2000])
  print(&quot;----------------------&quot;)
  print(obj.getData()[:2000])
  </code></pre>
  <p>The first print statement was different from the second print statement, which definitely should not be the case. To really test see if this was true, I replaced every single line with [()], which I know to be valid as there are many lines that are already that. For the life of me, though, I can't figure out <em>why</em> this function call fails to do any lasting changes.</p>
  <p>EDIT 2:</p>
  <p>I have further identified the problem. In the source code for an EncodedStreamObject in the getData method, it returns<code>self.decodedSelf.getData()</code> if self.decodedSelf is True. HOWEVER, after doing <code>obj.decodedSelf.setData(encoded_data)</code>, if I do <code>print(bool(obj.decodedSelf))</code>, it prints <code>False</code>. This means that when the EncodedStreamObject is getting accessed to be written out to the PDF, it is re-parsing the old PDF and overriding the <code>self.decodedSelf</code> object! Short of going in and fixing the source code, I'm not sure how I would solve this problem.</p>
  <p>EDIT 3:</p>
  <p>I have managed to convince the library to use the decoded version that has the replacements! By inserting the line <code>page[PyPDF2.pdf.NameObject(&quot;/Contents&quot;)] = contents.decodedSelf</code> before <code>writer.addPage(page)</code>, it forces the page to have the updated contents. Unfortunately, my previous assumption about the text kerning was incorrect. After I replaced things, some of my text mysteriously disappeared from the PDF. I assume this is because the format is incorrect somehow.</p>
  <p>FINAL EDIT:</p>
  <p>I figure I'd put this in here in case anyone else stumbles across this. I never did manage to get it to finally work as expected. I instead moved to a solution to mimic the PDF with HTML/CSS. If you add the following style tag in your HTML, you can get it to print more like how you'd expect a PDF to print:</p>
  <pre><code>&lt;style type=&quot;text/css&quot; media=&quot;print&quot;&gt;
      @page {
          size: auto;
          margin: 0;
      }
  &lt;/style&gt;
  </code></pre>
  <p>I'd recommend this solution for anyone looking to do what I was doing. There are Python libraries to convert HTML to CSS, but they do not support HTML5 and CSS3 (notably they do not support CSS flex or grid). You can just print the HTML page to PDF from any browser to accomplish the same thing. It definitely doesn't answer the question, so I felt it best to leave it as an edit. If anyone manages to complete what I have attempted, please post an answer for me and any others.</p>