page.insert_textbox() not working with strings containing spaces

I’m working on a script that will add employee names to a table in a PDF by first extracting that table, modifying it, and reentering it into the PDF. My script is as follows:

import camelot
import fitz
import pandas as pd

def extract_tables_from_pdf(pdf_path):
    try:
        tables = camelot.read_pdf(pdf_path, pages='all')
        print(f"Total tables extracted: {len(tables)}")
        
        pdf_document = fitz.open(pdf_path)
        
        header_coordinates = {}

        for i, table in enumerate(tables):
            print(f"--- Table {i + 1} ---")
            
            table.to_csv(f'table_{i + 1}.csv')
            
            page_num = table.page
            page = pdf_document.load_page(page_num - 1)
            
            header_texts = ["Print", "Sign", "Company"]
            
            for text in header_texts:
                text_instances = page.search_for(text)
                for inst in text_instances:
                    print(f"Header '{text}' found at page {page_num}, coordinates: {inst}")
                    header_coordinates[text] = inst
        
        pdf_document.close()
        return tables, header_coordinates
    
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None, None

def modify_and_insert_table(pdf_path, tables, header_coordinates, output_pdf_path):
    try:
        pdf_document = fitz.open(pdf_path)

        for i, table in enumerate(tables):
            df = table.df

            df_no_headers = df.iloc[1:].copy()
            
            new_data = pd.Series(["Employee Name", "Employee Name", "Company Name"], index=[0, 1, 2])
            df_no_headers.iloc[0, 0:3] = new_data
            
            table_text = df_no_headers.to_string(index=False, header=False)

            page_num = table.page
            page = pdf_document.load_page(page_num - 1)
            
            for col_idx, header in enumerate(["Print", "Sign", "Company"]):
                if header in header_coordinates:
                    inst = header_coordinates[header]
                    rect = fitz.Rect(inst.x0, inst.y1, inst.x1, inst.y1 + 20)
                    text = new_data[col_idx]
                      
                    page.insert_textbox(rect, text, fontsize=12, fontname="helvetica", color=(0, 0, 0), align=0)

        pdf_document.save(output_pdf_path)
        pdf_document.close()
        print(f"Modified PDF saved to {output_pdf_path}")
    
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    pdf_path = "/path/to/Adobe.pdf"
    output_pdf_path = "/path/to/Signed Adobe.pdf"
    
    tables, header_coordinates = extract_tables_from_pdf(pdf_path)
    if tables and header_coordinates:
        modify_and_insert_table(pdf_path, tables, header_coordinates, output_pdf_path)

I noticed that new_data = pd.Series(["Employee Name", "Employee Name", "Company Name"], index=[0, 1, 2]) didn’t work, so I tried new_data = pd.Series(["Employee", "Employee", "Company"], index=[0, 1, 2]) and that appears to have worked fine.

Right before page.insert_textbox(rect, text, fontsize=12, fontname="helvetica", color=(0, 0, 0), align=0), I put print(text) and am able to see Employee Name and Company Name, but when I inspect the Signed Adobe.pdf, the text is not there. I’m not sure why it would work with a single string containing no spaces, and not with a string containing spaces. Any assistance is greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật