46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
import csv
|
|
import zipfile
|
|
import io
|
|
|
|
def write_output_csv_to_zip(output_file, transformed_data):
|
|
output_headers = [
|
|
"Typ operace", "Datum", "ID transakce", "Částka", "Zůstatek na účtu", "Změnit zůstatek na účtu",
|
|
"ID objednávky", "Popis", "Popis2/Číslo účtu", "Město", "PSČ", "Telefon", "E-mail",
|
|
"Emailová adresa", "Jméno a příjmení", "Provize", "Měna"
|
|
]
|
|
|
|
# Create an in-memory ZIP archive
|
|
mem_zip = io.BytesIO()
|
|
with zipfile.ZipFile(mem_zip, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
for idx, file_data in enumerate(transformed_data):
|
|
# Create a filename, e.g. "0_output_file.csv"
|
|
filename = f"{idx}_{output_file}"
|
|
# Create an in-memory text stream for CSV output
|
|
csv_buffer = io.StringIO()
|
|
writer = csv.writer(csv_buffer, delimiter=';')
|
|
# Write header and file rows
|
|
writer.writerow(output_headers)
|
|
writer.writerows(file_data)
|
|
# Get CSV content as a string, encode it to bytes
|
|
csv_content = csv_buffer.getvalue().encode("utf-8")
|
|
csv_buffer.close()
|
|
# Write the CSV file into the ZIP archive
|
|
zf.writestr(filename, csv_content)
|
|
|
|
mem_zip.seek(0) # Reset pointer to the beginning of the ZIP archive
|
|
return mem_zip
|
|
|
|
def write_output_gpc_to_zip(output_file, transformed_data):
|
|
# Create an in-memory ZIP archive
|
|
mem_zip = io.BytesIO()
|
|
with zipfile.ZipFile(mem_zip, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
for idx, file_content in enumerate(transformed_data):
|
|
filename = f"{idx}_{output_file}"
|
|
# Check if file_content is a string; if so, encode it
|
|
if isinstance(file_content, str):
|
|
encoded_content = file_content.encode("windows-1250")
|
|
else:
|
|
encoded_content = file_content
|
|
zf.writestr(filename, encoded_content)
|
|
mem_zip.seek(0) # Reset pointer to the beginning of the ZIP archive
|
|
return mem_zip |