How to save Excel sheets to PDF using Python

0 votes

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?

Apr 10, 2023 in Others by Kithuzzz
• 38,000 points
6,108 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Others

0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4, 2022 in Others by narikkadan
• 63,600 points
4,182 views
0 votes
1 answer

How to convert excel to PDF using Python

Specify your whole output path instead of ...READ MORE

answered Nov 10, 2022 in Others by narikkadan
• 63,600 points
3,040 views
0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
14,045 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
1,397 views
0 votes
1 answer

How to paste an Excel chart into PowerPoint placeholder using Python?

Very near indeed! Copy and Paste are ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 63,600 points
4,305 views
0 votes
1 answer

How can I convert excel file to pdf using TCPDF?

PHPExcel can only read charts from Excel2007 ...READ MORE

answered Oct 21, 2022 in Others by narikkadan
• 63,600 points
2,171 views
0 votes
1 answer

Openpyxl password protect excel file python

Workbooks can be secured against certain actions, ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
8,313 views
0 votes
1 answer

How to save a new sheet in an existing excel file, using Pandas?

import pandas as pd import numpy as np path ...READ MORE

answered Dec 10, 2022 in Others by narikkadan
• 63,600 points
7,591 views
0 votes
1 answer

Python - Generate PDF from created Excel workbook - cannot save pdf document

The problem is that temp.xlsx doesn't shut ...READ MORE

answered Apr 6, 2023 in Others by narikkadan
• 63,600 points
1,046 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,020 points
1,118 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP