Ze101 commited on
Commit
ac5c3ba
·
1 Parent(s): 86c249c

fix docx bugs

Browse files
Files changed (2) hide show
  1. app.py +16 -8
  2. requirements.txt +1 -0
app.py CHANGED
@@ -5,25 +5,33 @@ from io import BytesIO
5
  import os
6
  from fpdf import FPDF
7
 
 
 
 
 
 
 
 
 
 
 
 
8
  def docx_to_pdf(docx_file):
9
  """
10
- Convert a DOCX file into a simple PDF format.
11
  """
12
  document = Document(docx_file)
13
- pdf = FPDF()
14
- pdf.set_auto_page_break(auto=True, margin=15)
15
- pdf.add_page()
16
- pdf.set_font("Arial", size=12)
17
 
18
- # Extract and add text from DOCX to the PDF
19
  for paragraph in document.paragraphs:
20
  text = paragraph.text.strip()
21
  if text:
22
- pdf.multi_cell(0, 10, text)
23
 
 
24
  pdf_bytes = BytesIO()
25
  pdf.output(pdf_bytes)
26
- pdf_bytes.seek(0) # Reset pointer
27
  return pdf_bytes
28
 
29
  def merge_files(file_list, output_filename="merged.pdf"):
 
5
  import os
6
  from fpdf import FPDF
7
 
8
+ class PDF(FPDF):
9
+ def __init__(self):
10
+ super().__init__()
11
+ self.add_page()
12
+ self.set_auto_page_break(auto=True, margin=15)
13
+ self.set_font("Arial", size=12)
14
+
15
+ def add_unicode_text(self, text):
16
+ # Handle multi-cell text with special characters
17
+ self.multi_cell(0, 10, text)
18
+
19
  def docx_to_pdf(docx_file):
20
  """
21
+ Convert a DOCX file into a simple PDF format, with Unicode support.
22
  """
23
  document = Document(docx_file)
24
+ pdf = PDF()
 
 
 
25
 
 
26
  for paragraph in document.paragraphs:
27
  text = paragraph.text.strip()
28
  if text:
29
+ pdf.add_unicode_text(text)
30
 
31
+ # Save to BytesIO
32
  pdf_bytes = BytesIO()
33
  pdf.output(pdf_bytes)
34
+ pdf_bytes.seek(0)
35
  return pdf_bytes
36
 
37
  def merge_files(file_list, output_filename="merged.pdf"):
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  fpdf
2
  PyPDF2
3
  Pillow
 
1
+ fpdf2
2
  fpdf
3
  PyPDF2
4
  Pillow