I have a multiple-sheet Excel spreadsheet. I require a Python script that will save each sheet as a PDF in landscape orientation, scaled to the pagewidth, with the sheet name serving as the PDF's filename. What I have so far thought of is as follows:
import pandas as pd
import openpyxl
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
# Load the Excel spreadsheet
file_name = 'A1 Marking Rubric 2023 in progress.xlsx'
xl = pd.ExcelFile(file_name)
# For each sheet in the workbook, create a PDF
for sheet_name in xl.sheet_names:
# Load the sheet into a DataFrame
df = pd.read_excel(file_name, sheet_name=sheet_name)
# Set the output file name
pdf_file_name = f"{sheet_name}"
# Get the sheet dimensions
wb = openpyxl.load_workbook(file_name)
ws = wb[sheet_name]
max_col = ws.max_column
max_row = ws.max_row
# Calculate the page width
page_width = sum(ws.column_dimensions[get_column_letter(
i)].width for i in range(1, max_col+1))
# Set the page margins
page_margins = PageMargins(left=0.25, right=0.25, top=0.75, bottom=0.75)
# Save the sheet as a PDF
with pd.ExcelWriter(pdf_file_name, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name=sheet_name, index=False)
wb = writer.book
ws = wb[sheet_name]
ws.page_margins = page_margins
ws.page_setup.fitToWidth = 1
ws.page_setup.fitToHeight = 0
# Move the PDF to the current directory
import os
os.replace(pdf_file_name, f"./{pdf_file_name}.pdf")
I end up with the right number of files in the target folder, with the right names, but each of them is File not in PDF format or corrupted. The other questions on this topic don't use Python. Can anyone see the fault?