Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,14 @@ from docx import Document
|
|
| 3 |
from docx.shared import Inches, Pt, RGBColor
|
| 4 |
from docx.enum.table import WD_TABLE_ALIGNMENT
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import os
|
| 8 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_image_creation_date(uploaded_file):
|
| 11 |
try:
|
|
@@ -59,10 +64,8 @@ def generate_report(images, job_number, job_name, job_address, uploader_name, la
|
|
| 59 |
|
| 60 |
# Set column widths
|
| 61 |
logo_width = Inches(1.5)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
header_table.columns[0].width = logo_width_int
|
| 65 |
-
header_table.columns[1].width = available_width_int - logo_width_int
|
| 66 |
|
| 67 |
# Insert logo in the first cell
|
| 68 |
logo_cell = header_table.cell(0, 0)
|
|
@@ -91,7 +94,7 @@ def generate_report(images, job_number, job_name, job_address, uploader_name, la
|
|
| 91 |
table.autofit = False
|
| 92 |
|
| 93 |
# Set column widths
|
| 94 |
-
col_width =
|
| 95 |
for col in table.columns:
|
| 96 |
col.width = col_width
|
| 97 |
|
|
@@ -175,10 +178,19 @@ def generate_report(images, job_number, job_name, job_address, uploader_name, la
|
|
| 175 |
paragraph.paragraph_format.space_before = Pt(0)
|
| 176 |
paragraph.paragraph_format.space_after = Pt(0)
|
| 177 |
|
| 178 |
-
# Save the document
|
| 179 |
-
|
| 180 |
-
doc.save(
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Streamlit app
|
| 184 |
st.markdown(
|
|
@@ -198,7 +210,7 @@ st.markdown(
|
|
| 198 |
)
|
| 199 |
|
| 200 |
# Upload multiple images
|
| 201 |
-
uploaded_images = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "jpeg", "png", "bmp", "tiff", "gif"])
|
| 202 |
|
| 203 |
# Layout selection placed after image upload
|
| 204 |
if uploaded_images:
|
|
@@ -221,16 +233,32 @@ uploader_name = st.text_input("Uploader Name")
|
|
| 221 |
# Generate report button
|
| 222 |
if st.button("Generate Report"):
|
| 223 |
if uploaded_images:
|
| 224 |
-
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
# Display confirmation
|
| 227 |
st.success("Your report is ready for download!")
|
| 228 |
-
|
|
|
|
|
|
|
| 229 |
st.download_button(
|
| 230 |
-
label="Download
|
| 231 |
data=file,
|
| 232 |
file_name="HKC_Report.docx",
|
| 233 |
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 234 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
else:
|
| 236 |
st.warning("No images uploaded. Please upload at least one image to generate a report.")
|
|
|
|
| 3 |
from docx.shared import Inches, Pt, RGBColor
|
| 4 |
from docx.enum.table import WD_TABLE_ALIGNMENT
|
| 5 |
from PIL import Image
|
| 6 |
+
import pillow_heif # Import pillow_heif to handle HEIC images
|
| 7 |
from datetime import datetime
|
| 8 |
import os
|
| 9 |
import io
|
| 10 |
+
import subprocess
|
| 11 |
+
|
| 12 |
+
# Register HEIF opener for PIL
|
| 13 |
+
pillow_heif.register_heif_opener()
|
| 14 |
|
| 15 |
def get_image_creation_date(uploaded_file):
|
| 16 |
try:
|
|
|
|
| 64 |
|
| 65 |
# Set column widths
|
| 66 |
logo_width = Inches(1.5)
|
| 67 |
+
header_table.columns[0].width = logo_width
|
| 68 |
+
header_table.columns[1].width = available_width - logo_width
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Insert logo in the first cell
|
| 71 |
logo_cell = header_table.cell(0, 0)
|
|
|
|
| 94 |
table.autofit = False
|
| 95 |
|
| 96 |
# Set column widths
|
| 97 |
+
col_width = (available_width - Inches(0.1)) / 2
|
| 98 |
for col in table.columns:
|
| 99 |
col.width = col_width
|
| 100 |
|
|
|
|
| 178 |
paragraph.paragraph_format.space_before = Pt(0)
|
| 179 |
paragraph.paragraph_format.space_after = Pt(0)
|
| 180 |
|
| 181 |
+
# Save the Word document
|
| 182 |
+
output_docx_path = "HKC_report_output.docx"
|
| 183 |
+
doc.save(output_docx_path)
|
| 184 |
+
|
| 185 |
+
# Convert the Word document to PDF using libreoffice
|
| 186 |
+
output_pdf_path = "HKC_report_output.pdf"
|
| 187 |
+
try:
|
| 188 |
+
subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', '--outdir', os.getcwd(), output_docx_path], check=True)
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"Error converting to PDF: {e}")
|
| 191 |
+
output_pdf_path = None
|
| 192 |
+
|
| 193 |
+
return output_docx_path, output_pdf_path
|
| 194 |
|
| 195 |
# Streamlit app
|
| 196 |
st.markdown(
|
|
|
|
| 210 |
)
|
| 211 |
|
| 212 |
# Upload multiple images
|
| 213 |
+
uploaded_images = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "jpeg", "png", "bmp", "tiff", "gif", "heic"])
|
| 214 |
|
| 215 |
# Layout selection placed after image upload
|
| 216 |
if uploaded_images:
|
|
|
|
| 233 |
# Generate report button
|
| 234 |
if st.button("Generate Report"):
|
| 235 |
if uploaded_images:
|
| 236 |
+
output_docx_path, output_pdf_path = generate_report(
|
| 237 |
+
uploaded_images, job_number, job_name, job_address, uploader_name, layout_style, compress_images
|
| 238 |
+
)
|
| 239 |
|
| 240 |
+
# Display confirmation
|
| 241 |
st.success("Your report is ready for download!")
|
| 242 |
+
|
| 243 |
+
# Provide download link for Word document
|
| 244 |
+
with open(output_docx_path, "rb") as file:
|
| 245 |
st.download_button(
|
| 246 |
+
label="Download Word Document",
|
| 247 |
data=file,
|
| 248 |
file_name="HKC_Report.docx",
|
| 249 |
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 250 |
)
|
| 251 |
+
|
| 252 |
+
# Provide download link for PDF document if conversion was successful
|
| 253 |
+
if output_pdf_path and os.path.exists(output_pdf_path):
|
| 254 |
+
with open(output_pdf_path, "rb") as file:
|
| 255 |
+
st.download_button(
|
| 256 |
+
label="Download PDF Document",
|
| 257 |
+
data=file,
|
| 258 |
+
file_name="HKC_Report.pdf",
|
| 259 |
+
mime="application/pdf"
|
| 260 |
+
)
|
| 261 |
+
else:
|
| 262 |
+
st.warning("PDF conversion failed or is not available.")
|
| 263 |
else:
|
| 264 |
st.warning("No images uploaded. Please upload at least one image to generate a report.")
|