admin管理员组文章数量:1024158
I am interested to change the font of some text that is in documents.
I used the #581-filtering-by-font example and added re-submitting to SimpleFindReplace and it is triggering the assertion that a rectangle cannot have a negative width.
I have confirmed the initial font 'VFMBOX+HelveticaNeueLTStd-Roman' is in the document.
Borb version 2.1.25
Python version - 3.11
This is the code -
*`#!chapter_005/src/snippet_017.py
import typing
from borb.pdf import Document
from borb.pdf import PDF
from borb.toolkit import FontNameFilter
from borb.toolkit import SimpleTextExtraction
from borb.toolkit import SimpleFindReplace
def main():
# create FontNameFilter
l0: FontNameFilter = FontNameFilter("VFMBOX+HelveticaNeueLTStd-Roman")
# filtered text just gets passed to SimpleTextExtraction
l1: SimpleTextExtraction = SimpleTextExtraction()
l0.add_listener(l1)
# read the Document
doc: typing.Optional[Document] = None
with open("C:\Data\PDFFontChange\PD-1239-G_RevA_PR.pdf", "rb") as in_file_handle:
doc = PDF.loads(in_file_handle, [l0])
# check whether we have read a Document
assert doc is not None
templist = (l1.get_text()[0]).splitlines()
# print(templist)
i = 0
for x in templist :
print(i, ' : ', x)
i += 1
doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")
# store
with open("output2.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()
Here is the full error traceback & I have included the first numbered (printed as 0 : ) text found by the FontNameFilter from the PDF that is resubmitted by the code into the SimpleFindReplace that triggers the error.
0 : At the heart of your pump or dispenser,
Traceback (most recent call last):
File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 43, in <module>
main()
File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 36, in main
doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\simple_find_replace.py", line 72, in sub
] = RegularExpressionTextExtraction.get_matches_for_pdf(pattern, doc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 375, in get_matches_for_pdf
CanvasStreamProcessor(page, Canvas(), []).read(page_source, [cse])
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 277, in read
raise e
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 271, in read
operator.invoke(self, operands, event_listeners)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\operator\text\show_text_with_glyph_positioning.py", line 84, in invoke
l._event_occurred(tri)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 326, in _event_occurred
self._render_text(event)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 338, in _render_text
for e in text_render_info.split_on_glyphs():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\event\chunk_of_text_render_event.py", line 177, in split_on_glyphs
e._baseline_bounding_box = Rectangle(
^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\geometry\rectangle.py", line 30, in __init__
assert width >= 0, "A Rectangle must have a non-negative width."
^^^^^^^^^^
AssertionError: A Rectangle must have a non-negative width.
I am interested to change the font of some text that is in documents.
I used the #581-filtering-by-font example and added re-submitting to SimpleFindReplace and it is triggering the assertion that a rectangle cannot have a negative width.
I have confirmed the initial font 'VFMBOX+HelveticaNeueLTStd-Roman' is in the document.
Borb version 2.1.25
Python version - 3.11
This is the code -
*`#!chapter_005/src/snippet_017.py
import typing
from borb.pdf import Document
from borb.pdf import PDF
from borb.toolkit import FontNameFilter
from borb.toolkit import SimpleTextExtraction
from borb.toolkit import SimpleFindReplace
def main():
# create FontNameFilter
l0: FontNameFilter = FontNameFilter("VFMBOX+HelveticaNeueLTStd-Roman")
# filtered text just gets passed to SimpleTextExtraction
l1: SimpleTextExtraction = SimpleTextExtraction()
l0.add_listener(l1)
# read the Document
doc: typing.Optional[Document] = None
with open("C:\Data\PDFFontChange\PD-1239-G_RevA_PR.pdf", "rb") as in_file_handle:
doc = PDF.loads(in_file_handle, [l0])
# check whether we have read a Document
assert doc is not None
templist = (l1.get_text()[0]).splitlines()
# print(templist)
i = 0
for x in templist :
print(i, ' : ', x)
i += 1
doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")
# store
with open("output2.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()
Here is the full error traceback & I have included the first numbered (printed as 0 : ) text found by the FontNameFilter from the PDF that is resubmitted by the code into the SimpleFindReplace that triggers the error.
0 : At the heart of your pump or dispenser,
Traceback (most recent call last):
File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 43, in <module>
main()
File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 36, in main
doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\simple_find_replace.py", line 72, in sub
] = RegularExpressionTextExtraction.get_matches_for_pdf(pattern, doc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 375, in get_matches_for_pdf
CanvasStreamProcessor(page, Canvas(), []).read(page_source, [cse])
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 277, in read
raise e
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 271, in read
operator.invoke(self, operands, event_listeners)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\operator\text\show_text_with_glyph_positioning.py", line 84, in invoke
l._event_occurred(tri)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 326, in _event_occurred
self._render_text(event)
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 338, in _render_text
for e in text_render_info.split_on_glyphs():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\event\chunk_of_text_render_event.py", line 177, in split_on_glyphs
e._baseline_bounding_box = Rectangle(
^^^^^^^^^^
File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\geometry\rectangle.py", line 30, in __init__
assert width >= 0, "A Rectangle must have a non-negative width."
^^^^^^^^^^
AssertionError: A Rectangle must have a non-negative width.
版权声明:本文标题:python - Borb text triggers "AssertionError: A Rectangle must have a non-negative width." - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745591450a2157911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论