Export Charts from Excel as images using Python

I had to look at some VBA examples to get this working. Although I hate answering my own questions, I am leaving this here for people who might need it. import win32com.client as win32 wb = excel.Workbooks.Open(excel_file) selection = “A1:J30″ xl_range = wb.Sheets(<sheet_name>).Range(selection) excel.ActiveWorkbook.Sheets.Add( After=excel.ActiveWorkbook.Sheets(3)).Name=”image_sheet” cht = excel.ActiveSheet.ChartObjects().Add(0,0, xl_range.Width, xl_range.Height) xl_range.CopyPicture() # add the chart … Read more

python-win32com excel com model started generating errors

I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html Deleting the gen_py output directory and re-running makepy SUCCEEDS and subsequently the test application runs OK again. So the symptom is resolved, but any clues as to how this could have happened. This is a VERY long running application (think … Read more

Python – Converting XLSX to PDF

As my original answer was deleted and is eventually a bit useful, I repost it here. You could do it in 3 steps: excel to pandas: pandas.read_excel pandas to HTML: pandas.DataFrame.to_html HTML to pdf: python-pdfkit (git), python-pdfkit (pypi.org) import pandas as pd import pdfkit df = pd.read_excel(“file.xlsx”) df.to_html(“file.html”) pdfkit.from_file(“file.html”, “file.pdf”) install: sudo pip3.6 install pandas … Read more

.xlsx and xls(Latest Versions) to pdf using python

Link of xlsxwriter : https://xlsxwriter.readthedocs.org/en/latest/contents.html With the help of this you can generate excel file with .xlsx and .xls for example excel file generated name is trial.xls Now if you want to generate pdf of that excel file then do the following : from win32com import client xlApp = client.Dispatch(“Excel.Application”) books = xlApp.Workbooks.Open(‘C:\\excel\\trial.xls’) ws = … Read more

Using win32com with multithreading

If you want to use win32com in multiple threads you need to do a little bit more work as COMObject cannot be passed to a thread directly. You need to use CoMarshalInterThreadInterfaceInStream() and CoGetInterfaceAndReleaseStream() to pass instance between threads: import pythoncom, win32com.client, threading, time def start(): # Initialize pythoncom.CoInitialize() # Get instance xl = win32com.client.Dispatch(‘Excel.Application’) … Read more

.doc to pdf using python

A simple example using comtypes, converting a single file, input and output filenames given as commandline arguments: import sys import os import comtypes.client wdFormatPDF = 17 in_file = os.path.abspath(sys.argv[1]) out_file = os.path.abspath(sys.argv[2]) word = comtypes.client.CreateObject(‘Word.Application’) doc = word.Documents.Open(in_file) doc.SaveAs(out_file, FileFormat=wdFormatPDF) doc.Close() word.Quit() You could also use pywin32, which would be the same except for: import … Read more