-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
333 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from .docx2pdf import DocxToPDF | ||
from .docx2pdf import DocxToPDF, DocxToPDFV1 | ||
from .excel2pdf import ExcelToPDF | ||
from .pptx2pdf import PptxToPDF | ||
|
||
__all__ = ["DocxToPDF"] | ||
__all__ = ["DocxToPDF", "DocxToPDFV1", "PptxToPDF", "ExcelToPDF"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
import openpyxl | ||
|
||
|
||
class ExcelToPDF(object): | ||
def __init__(self, kwargs={}): | ||
cmd_template = """ | ||
soffice --convert-to | ||
"pdf:calc_pdf_Export:{\"SinglePageSheets\":{\"type\":\"boolean\",\"value\":\"true\"}}" | ||
--outdir | ||
""" | ||
cmd_template2 = """ | ||
soffice --headless --convert-to xlsx --outdir | ||
""" | ||
|
||
def _norm_cmd(cmd): | ||
return " ".join([p.strip() for p in cmd.strip().split()]) | ||
|
||
self.cmd_template = _norm_cmd(cmd_template) | ||
self.cmd_template2 = _norm_cmd(cmd_template2) | ||
|
||
@staticmethod | ||
def run(cmd): | ||
try: | ||
exit_code = os.system(cmd) | ||
if exit_code != 0: | ||
raise Exception("error in transforming xlsx to pdf") | ||
except Exception as e: | ||
raise e | ||
|
||
def render(self, input_file, output_file=None, to_bytes=False): | ||
type_ext = input_file.rsplit(".", 1)[-1] | ||
filename = os.path.basename(input_file) | ||
output_filename = filename.rsplit(".", 1)[0] + ".pdf" | ||
|
||
assert type_ext in ["xlsx", "xls"] | ||
|
||
with tempfile.TemporaryDirectory() as temp_dir: | ||
if type_ext == "xls": | ||
cmd = self.cmd_template2 + " {1} {0}".format(input_file, temp_dir) | ||
ExcelToPDF.run(cmd) | ||
filename = filename.rsplit(".", 1)[0] + ".xlsx" | ||
input_file = os.path.join(temp_dir, filename) | ||
|
||
print("input_file", input_file) | ||
temp_output_file = os.path.join(temp_dir, output_filename) | ||
wb = openpyxl.load_workbook(input_file) | ||
for ws in wb: | ||
ws.print_options.gridLines = True | ||
ws.print_options.gridLinesSet = True | ||
|
||
input_file = os.path.join(temp_dir, filename) | ||
wb.save(input_file) | ||
|
||
cmd = self.cmd_template + " {1} {0}".format(input_file, temp_dir) | ||
ExcelToPDF.run(cmd) | ||
|
||
if output_file is not None: | ||
shutil.move(temp_output_file, output_file) | ||
|
||
if to_bytes: | ||
return open(temp_output_file, "rb").read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import shutil | ||
|
||
|
||
class PptxToPDF(object): | ||
def __init__(self, kwargs={}): | ||
cmd_template = """ | ||
soffice --headless --convert-to pdf --outdir {1} {0} | ||
""" | ||
|
||
def _norm_cmd(cmd): | ||
return " ".join([p.strip() for p in cmd.strip().split()]) | ||
|
||
self.cmd_template = _norm_cmd(cmd_template) | ||
|
||
def render(self, input_file, output_file=None, to_bytes=False): | ||
type_ext = input_file.rsplit(".", 1)[-1] | ||
filename = os.path.basename(input_file) | ||
temp_dir = os.path.dirname(input_file) | ||
output_filename = filename.rsplit(".", 1)[0] + ".pdf" | ||
temp_output_file = os.path.join(temp_dir, output_filename) | ||
|
||
assert type_ext in ["pptx", "ppt"] | ||
|
||
cmd = self.cmd_template.format(input_file, temp_dir) | ||
try: | ||
exit_code = os.system(cmd) | ||
if exit_code != 0: | ||
raise Exception("error in transforming pptx to pdf") | ||
except Exception as e: | ||
raise e | ||
|
||
if output_file is not None: | ||
shutil.move(temp_output_file, output_file) | ||
|
||
if to_bytes: | ||
return open(temp_output_file, "rb").read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
|
||
|
||
class Text2PDF(object): | ||
def __init__(self, kwargs={}): | ||
cmd_template = """ | ||
pandoc -o {1} --pdf-engine=xelatex {0} | ||
-V mainfont="Alibaba PuHuiTi" | ||
-V sansfont="Alibaba PuHuiTi" | ||
-V monofont="Cascadia Mono" | ||
-V CJKmainfont="Alibaba PuHuiTi" | ||
-V CJKsansfont="Alibaba PuHuiTi" | ||
-V CJKmonofont="Adobe Heiti Std" | ||
""" | ||
|
||
self.cmd_template2 = "pandoc -o {1} {0}" | ||
|
||
def _norm_cmd(cmd): | ||
return " ".join([p.strip() for p in cmd.strip().split()]) | ||
|
||
self.cmd_template = _norm_cmd(cmd_template) | ||
self.cmd_template2 = "pandoc -o {1} {0}" | ||
|
||
@staticmethod | ||
def run(cmd): | ||
try: | ||
exit_code = os.system(cmd) | ||
if exit_code != 0: | ||
raise Exception("error in transforming xlsx to pdf") | ||
except Exception as e: | ||
raise e | ||
|
||
def render(self, input_file, output_file=None, to_bytes=False): | ||
type_ext = input_file.rsplit(".", 1)[-1] | ||
filename = os.path.basename(input_file) | ||
temp_dir = os.path.dirname(input_file) | ||
|
||
assert type_ext in ["txt", "md", "html"] | ||
|
||
if type_ext == "txt": | ||
cmd = self.cmd_template2.format(input_file, "./data/xxxx.md") | ||
Text2PDF.run(cmd) | ||
input_file = "./data/xxxx.md" | ||
|
||
if output_file is None: | ||
output_filename = filename.rsplit(".", 1)[0] + ".pdf" | ||
output_file = os.path.join(temp_dir, output_filename) | ||
|
||
cmd = self.cmd_template.format(input_file, output_file) | ||
Text2PDF.run(cmd) | ||
|
||
if to_bytes: | ||
return open(output_file, "rb").read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from bisheng_unstructured.topdf.excel2pdf import ExcelToPDF | ||
|
||
|
||
def test1(): | ||
input_file = "./examples/docs/tests-example.xlsx" | ||
output_file = "./data/tests-example-v1.0.pdf" | ||
engine = ExcelToPDF() | ||
engine.render(input_file, output_file) | ||
|
||
|
||
def test2(): | ||
input_file = "./examples/docs/tests-example.xls" | ||
output_file = "./data/tests-example-v1.1.pdf" | ||
engine = ExcelToPDF() | ||
engine.render(input_file, output_file) | ||
|
||
|
||
test1() | ||
test2() |
Oops, something went wrong.