Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Problema de decodificación de PyPDF2 al agregar anotaciones en caracteres chinos con addJS

Quiero usar PyPDF2 para agregar anotaciones programáticamente con el uso de addJS, funciona muy bien para caracteres latinos pero no para caracteres chinos, traté de codificar con UTF-8 pero parece que tampoco funciona. Aquí está el código:

 from PyPDF2 import PdfFileWriter, PdfFileReader Def Test(): inputPDF = PdfFileReader('./demo/TESTPDFANNOTATION.pdf', "rb") outputPDF = PdfFileWriter() pages = inputPDF.getNumPages() for p in range(pages): outputPDF.addPage(inputPDF.getPage(p)) outputStream = open('./demo/TESTPDFANNOTATIONOUT.pdf', "wb") outputPDF.addJS("var annot = this.addAnnot({ \r \ page: 0, \r \ type: 'FreeText', \r \ contents: '你好', \r \ textFont: 'csongl', \r \ textSize: 10, \r \ rect: [200, 300, 200+150, 300+3*12], // height for three lines \r \ width: 1, \r \ alignment: 1 \r \ });") outputPDF.write(outputStream) outputStream.close() return("ok")

Es extraño que si abrí el PDF en el editor de texto del bloc de notas, los caracteres chinos se mostraran correctamente; sin embargo, cuando se abre con PDF, muestra algo como 佀好, que parece no estar decodificado, ya que podrían decodificarse con la herramienta de conversión en línea en casi el carácter chino correcto, no exactamente igual para algunos casos. https://cafewebmaster.com/online_tools/utf_decode

¡Cualquier consejo sería muy apreciado!

Versión de Python: 3.9+ SO: Win10

gracias stanley

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finalmente, descubrí usar otro paquete PyMuPDF para agregar anotaciones mediante programación con un buen soporte de caracteres chinos.

 import fitz def writeAnnotation(): blue = (0,0,1) gold = (1,1,0) pdfDoc = fitz.open('./demo/TESTPDFANNOTATION.pdf') page = pdfDoc[0] rect1 = fitz.Rect(100,100,200,150) strContent1= "你好!世界" a1 = page.addFreetextAnnot(rect1, strContent1, text_color=blue, fill_color=gold) pdfDoc.save("./demo/TESTPDFANNOTATIONOUT.pdf") return("Well done!")
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!