-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Daethyra/v3.2-conversion_extensions
V3.2 conversion extensions
- Loading branch information
Showing
5 changed files
with
128 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
import os | ||
import pandas as pd | ||
import pypandoc | ||
import img2pdf | ||
from pdf2image.pdf2image import convert_from_path | ||
import fitz | ||
|
||
|
||
class Converter: | ||
def __init__(self, input_file): | ||
self.input_file = input_file | ||
self.input_extension = os.path.splitext(input_file)[1].lower() | ||
|
||
def to_pdf(self, output_file): | ||
if self.input_extension in ['.pdf', '.PDF']: | ||
shutil.copy(self.input_file, output_file) | ||
elif self.input_extension in ['.jpg', '.jpeg', '.png', '.bmp']: | ||
with open(output_file, "wb") as pdf_file: | ||
pdf_bytes = img2pdf.convert(self.input_file) | ||
if pdf_bytes: | ||
pdf_file.write(pdf_bytes) | ||
else: | ||
raise ValueError("Empty output") | ||
else: | ||
pypandoc.convert_file(self.input_file, 'pdf', outputfile=output_file, extra_args=['--pdf-engine', 'pdflatex', '--quiet']) | ||
|
||
def to_json(self, output_file): | ||
if self.input_extension in ['.html', '.htm']: | ||
df = pd.read_html(self.input_file)[0] | ||
df.to_json(output_file, orient='records') | ||
else: | ||
raise ValueError("Conversion to JSON is not supported for this file type") | ||
|
||
def to_csv(self, output_file): | ||
if self.input_extension in ['.html', '.htm']: | ||
df = pd.read_html(self.input_file)[0] | ||
df.to_csv(output_file, index=False) | ||
else: | ||
raise ValueError("Conversion to CSV is not supported for this file type") | ||
|
||
def to_yaml(self, output_file): | ||
if self.input_extension in ['.html', '.htm']: | ||
df = pd.read_html(self.input_file)[0] | ||
df.to_csv(output_file, index=False) | ||
else: | ||
raise ValueError("Conversion to YAML is not supported for this file type") |
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,55 @@ | ||
import os | ||
import sys | ||
import logging | ||
from concurrent.futures import ThreadPoolExecutor | ||
from main import input_file, output_format | ||
from extensions import Converter | ||
|
||
logging.basicConfig(filename='converter.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
||
|
||
def process_file(file_path, output_format): | ||
try: | ||
input_file = os.path.abspath(file_path) | ||
input_filename = os.path.splitext(input_file)[0] | ||
output_file = f"{input_filename}_output{output_format}" | ||
converter = Converter(input_file) | ||
if output_format == '.pdf': | ||
converter.to_pdf(output_file) | ||
elif output_format == '.json': | ||
converter.to_json(output_file) | ||
elif output_format == '.csv': | ||
converter.to_csv(output_file) | ||
elif output_format == '.yaml': | ||
converter.to_yaml(output_file) | ||
else: | ||
raise ValueError(f"Unsupported output format: {output_format}") | ||
logging.info(f"Successfully converted {input_file} to {output_file}") | ||
except Exception as e: | ||
logging.error(f"Error converting {input_file}: {str(e)}") # type: ignore | ||
|
||
|
||
def batch_process(directory_path, output_format, max_workers=4): | ||
with ThreadPoolExecutor(max_workers=max_workers) as executor: | ||
for root, _, files in os.walk(directory_path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
executor.submit(process_file, file_path, output_format) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 3: | ||
print("Usage: python utility.py <directory_path> <output_format>") | ||
sys.exit(1) | ||
|
||
directory_path = sys.argv[1] | ||
output_format = sys.argv[2].lower() | ||
|
||
if not output_format.startswith('.'): | ||
output_format = '.' + output_format | ||
|
||
if not os.path.exists(directory_path) or not os.path.isdir(directory_path): | ||
print(f"Directory '{directory_path}' does not exist.") | ||
sys.exit(1) | ||
|
||
batch_process(directory_path, output_format) |