Spaces:
Sleeping
Sleeping
Update src/pyscripts/file_process.py
Browse files- src/pyscripts/file_process.py +16 -10
src/pyscripts/file_process.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import io
|
|
|
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
|
| 4 |
pdf_path_dict = {
|
|
@@ -8,8 +9,11 @@ pdf_path_dict = {
|
|
| 8 |
"演讲": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
| 9 |
"致辞": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
| 10 |
}
|
| 11 |
-
font_path = "src/assets/
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
# font = fitz.Font(fontfile=font_path)
|
| 14 |
|
| 15 |
def getInvitationPDF(data):
|
|
@@ -25,27 +29,29 @@ def getInvitationPDF(data):
|
|
| 25 |
pdfStream = io.BytesIO()
|
| 26 |
doc.save(pdfStream)
|
| 27 |
doc.close()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
return pdfStream
|
| 31 |
|
| 32 |
|
| 33 |
-
def writeInvitationPDF(person
|
| 34 |
font_size=30
|
| 35 |
pdf_path = pdf_path_dict[person.category]
|
| 36 |
doc = fitz.open(pdf_path)
|
| 37 |
page = doc.load_page(1)
|
|
|
|
| 38 |
page.insert_font(fontname="f", fontfile=font_path)
|
| 39 |
page.insert_text((145, 80),
|
| 40 |
-
person.name+person.title, fontname="
|
| 41 |
if person.category == "演讲" or person.category == "致辞":
|
| 42 |
page.insert_text((80, 375),
|
| 43 |
person.conference, fontname="f", fontsize=min(font_size, font_size*14/len(person.conference)), color=(1, 1, 1))
|
| 44 |
pdfStream = io.BytesIO()
|
| 45 |
doc.save(pdfStream)
|
| 46 |
doc.close()
|
| 47 |
-
if
|
| 48 |
-
with open("output/output.pdf", 'wb') as f:
|
| 49 |
f.write(pdfStream.getvalue())
|
| 50 |
return pdfStream
|
| 51 |
|
|
@@ -53,11 +59,11 @@ def writeInvitationPDF(person, debug=False):
|
|
| 53 |
if __name__ == "__main__":
|
| 54 |
from collections import namedtuple
|
| 55 |
Person = namedtuple('Person', ['name', 'title', 'category', 'conference', 'respond_person'])
|
| 56 |
-
person = Person(name="
|
| 57 |
|
| 58 |
output_path = 'output/output.pdf'
|
| 59 |
|
| 60 |
# getInvitationPDF(data)
|
| 61 |
-
writeInvitationPDF(person
|
| 62 |
# with open(output_path, 'wb') as f:
|
| 63 |
# f.write(pdfStream.getvalue())
|
|
|
|
| 1 |
import io
|
| 2 |
+
import os
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
|
| 5 |
pdf_path_dict = {
|
|
|
|
| 9 |
"演讲": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
| 10 |
"致辞": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
| 11 |
}
|
| 12 |
+
# font_path = "src/assets/NotoSansHans-Regular.otf"
|
| 13 |
+
# other_font_path = "src/assets/SourceHanSansCN-VF-2.otf"
|
| 14 |
+
# other_font_path = "src/assets/ZhouZiSongTi7000Zi-2.otf"
|
| 15 |
+
font_path = "src/assets/字魂59号-创粗黑.ttf"
|
| 16 |
+
other_font_path = "src/assets/FangZhengHeiTiJianTi-1.ttf"
|
| 17 |
# font = fitz.Font(fontfile=font_path)
|
| 18 |
|
| 19 |
def getInvitationPDF(data):
|
|
|
|
| 29 |
pdfStream = io.BytesIO()
|
| 30 |
doc.save(pdfStream)
|
| 31 |
doc.close()
|
| 32 |
+
if os.path.exists("src/assets/output"):
|
| 33 |
+
with open("src/assets/output/output.pdf", 'wb') as f:
|
| 34 |
+
f.write(pdfStream.getvalue())
|
| 35 |
return pdfStream
|
| 36 |
|
| 37 |
|
| 38 |
+
def writeInvitationPDF(person):
|
| 39 |
font_size=30
|
| 40 |
pdf_path = pdf_path_dict[person.category]
|
| 41 |
doc = fitz.open(pdf_path)
|
| 42 |
page = doc.load_page(1)
|
| 43 |
+
page.insert_font(fontname="name_font", fontfile=font_path)
|
| 44 |
page.insert_font(fontname="f", fontfile=font_path)
|
| 45 |
page.insert_text((145, 80),
|
| 46 |
+
person.name+person.title, fontname="name_font", fontsize=min(font_size, font_size*5/len(person.name+person.title)), color=(1, 1, 1))
|
| 47 |
if person.category == "演讲" or person.category == "致辞":
|
| 48 |
page.insert_text((80, 375),
|
| 49 |
person.conference, fontname="f", fontsize=min(font_size, font_size*14/len(person.conference)), color=(1, 1, 1))
|
| 50 |
pdfStream = io.BytesIO()
|
| 51 |
doc.save(pdfStream)
|
| 52 |
doc.close()
|
| 53 |
+
if os.path.exists("src/assets/output"):
|
| 54 |
+
with open("src/assets/output/output.pdf", 'wb') as f:
|
| 55 |
f.write(pdfStream.getvalue())
|
| 56 |
return pdfStream
|
| 57 |
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
from collections import namedtuple
|
| 61 |
Person = namedtuple('Person', ['name', 'title', 'category', 'conference', 'respond_person'])
|
| 62 |
+
person = Person(name="周鸿祎", title="先生", category="致辞", conference="8月21日中欧智慧论坛,做主题演讲", respond_person="Eazy")
|
| 63 |
|
| 64 |
output_path = 'output/output.pdf'
|
| 65 |
|
| 66 |
# getInvitationPDF(data)
|
| 67 |
+
writeInvitationPDF(person)
|
| 68 |
# with open(output_path, 'wb') as f:
|
| 69 |
# f.write(pdfStream.getvalue())
|