How to make an URL 'clickable' in Excel with Python? [duplicate] - python

I am using win32com to modify an Excel spreadsheet (Both read and edit at the same time) I know there are other modules out there that can do one or the other but for the application I am doing I need it read and processed at the same time.
The final step is to create some hyperlinks off of a path name. Here is an Example of what I have so far:
import win32com.client
excel = r'I:\Custom_Scripts\Personal\Hyperlinks\HyperlinkTest.xlsx'
xlApp = win32com.client.Dispatch("Excel.Application")
workbook = xlApp.Workbooks.Open(excel)
worksheet = workbook.Worksheets("Sheet1")
for xlRow in xrange(1, 10, 1):
a = worksheet.Range("A%s"%(xlRow)).Value
if a == None:
break
print a
workbook.Close()
I found some code for reading Hyperlinks using win32com:
sheet.Range("A8").Hyperlinks.Item(1).Address
but not how to set hyperlinks
Can someone assist me?

Borrowing heavily from this question, as I couldn't find anything on SO to link to as a duplicate...
This code will create a Hyperlink in cells A1:A9
import win32com.client
excel = r'I:\Custom_Scripts\Personal\Hyperlinks\HyperlinkTest.xlsx'
xlApp = win32com.client.Dispatch("Excel.Application")
workbook = xlApp.Workbooks.Open(excel)
worksheet = workbook.Worksheets("Sheet1")
for xlRow in xrange(1, 10, 1):
worksheet.Hyperlinks.Add(Anchor = worksheet.Range('A{}'.format(xlRow)),
Address="http://www.microsoft.com",
ScreenTip="Microsoft Web Site",
TextToDisplay="Microsoft")
workbook.Save()
workbook.Close()
And here is a link to the Microsoft Documentation for the Hyperlinks.Add() method.

Related

Print specific sheets in excel doc to PDF with Python (xlwings)

I am attempting to automate the very manual process of individually selecting a range of worksheets within an excel file to PDF. I was able to string together the following code, which successfully prints the document. However, I cannot figure out how to select specific worksheets within my workbook, so it currently prints the entire workbook to PDF (which comes out to a whopping 897 pages).
Any ideas on how to select certain pages and then print to PDF with a given file name?
import os
import xlwings as xw
book = xw.Book(r'linktomyfile.xlsm')
sheet = book.sheets[0]
current_work_dir = os.getcwd()
pdf_path = os.path.join(current_work_dir, "Report_Date.pdf")
print(f"Saving workbook as '{pdf_path}' ...")
book.api.ExportAsFixedFormat(0, pdf_path)
print(f"Opening PDF file with default application")
Much appreciated!
You can just use the sheet reference to print to pdf, for example:
book = xw.Book(r'linktomyfile.xlsm')
sheet = book.sheets("Sheet1")
current_work_dir = os.getcwd()
pdf_path = os.path.join(current_work_dir, "Report_Date.pdf")
sheet.api.ExportAsFixedFormat(0, pdf_path)
You can also specify a range, e.g.
sheet.range("A1:G15").api.ExportAsFixedFormat(0, pdf_path)
Example of looping through specific sheets:
sheetlist = ["Sheet A", "Sheet B"]
for each in sheetlist:
pdf_path = os.path.join(current_work_dir, f"{each}.pdf")
sht = book.sheets(each)
sht.api.ExportAsFixedFormat(0, pdf_path)
Here each pdf is named after the sheet name.
In newer version of xlwings, there's a built in .to_pdf() function. Assuming you've got a book or sheet ready to print:
# to print a whole workbook
myXlwingsWorkBook.to_pdf(r"c:\myOutputPath")
# print a sheet
myXlwingsSheet.to_pdf(r"c:\myOutputPath")
Documentation: Xlwings documentation - then search for "PDF"
There're a few options. I wish I could just print/pdf the first page though...

Open and modify specific worksheet in excel workbook

I am trying to open and format a specific Excel worksheet. However I am having trouble trying to find out how to look at a specific worksheet.
The code I'm trying to use to open the workbook and go to a specific worksheet and then change the font for specific cells is:
from openpyxl import Workbook
def applyValidations(path,tabname):
workbook = Workbook(path)
worksheet = workbook[tabname]
c = worksheet['A1:A5']
c.font = Font(size=22)
The error I'm getting is:
KeyError: 'Worksheet Department Data does not exist.'
Department Data is the name of the worksheet which does exist in the workbook.
Here's some code I use regularly with openpyxl, which may solve your Q:
from openpyxl import load_workbook
wb = load_workbook(filename=data_file, read_only=True)
ws = wb.active
print(wb.sheetnames)

How do I download an xlsm file and read every sheet in python?

Right now I am doing the following.
import xlrd
resp = requests.get(url, auth=auth).content
output = open(r'temp.xlsx', 'wb')
output.write(resp)
output.close()
xl = xlrd.open_workbook(r'temp.xlsx')
sh = 1
try:
for sheet in xl.sheets():
xls.append(sheet.name)
except:
xls = ['']
It's extracting the sheets but I don't know how to read the file or if saving the file as an .xlsx is actually working for macros. All I know is that the code is not working right now and I need to be able to catch the data that is being generated in a macro. Please help! Thanks.
I highly recommend using xlwings if you want to open, modify, and save .xlsm files without corrupting them. I have tried a ton of different methods (using other modules like openpyxl) and the macros always end up being corrupted.
import xlwings as xw
app = xw.App(visible=False) # IF YOU WANT EXCEL TO RUN IN BACKGROUND
xlwb = xw.Book('PATH\\TO\\FILE.xlsm')
xlws = {}
xlws['ws1'] = xlwb.sheets['Your Worksheet']
print(xlws['ws1'].range('B1').value) # get value
xlws['ws1'].range('B1').value = 'New Value' # change value
yourMacro = xlwb.macro('YourExcelMacro')
yourMacro()
xlwb.save()
xlwb.close()
Edit - I added an option to keep Excel invisible at users request

Accessing UpdateLinks() in COM Object using Python

I am working on automating an Excel file which is linked to certain .csv files.
Those .csv files are created from a SAS Code which is run every Quarter.
The files created are timestamped accordingly for example XYZ_201603.csv and XYZ_201606.csv and so on.
I need to update the links on my Excel File so that it automatically changes the link to the file from next quarter. I am trying to do this using Python win32com.client and my code looks like
from win32com import Dispatch
xl_app = Dispatch("Excel.Application")
xl_app.Visible = True
xl_app.DisplayAlerts = False
wb = xl_app.workbooks.open(r"C:\Users\XYZ\Desktop\Test\Summary.xlsx")
xl_app.AskToUpdateLinks = False
try:
wb.UpdateLink(Name=r"C:\Users\XYZ\Desktop\Test\XYZ_201606.csv")
except Exception as e:
print(e)
finally:
wb.Close(True)
wb = None
return True
xl_app.Quit()
xl = None
Whenever I run this, I get the following error
(-2147352567,'Exception occured.',(0,'Microsoft Excel','UpdateLink method of
Workbook class failed','xlmain11.chm',0,-2146827284),None)
Can Somebody tell me what is going wrong here. Also, incase I have multiple links, how do I tell which link needs to be changed to what? Can I pass a dictionary of directories of updated datasets
The code and the approach has been taken from this answer on Stack Overflow
Update Links in for Excel Spreadsheet Using Python
If you review the Microsoft Documentation, it seems that the UpdateLink method can be called without any parameters. Therefore this program should work:
import win32com.client as win32
xl_app = win32.gencache.EnsureDispatch("Excel.Application")
xl_app.Visible = True
xl_app.DisplayAlerts = False
wb = xl_app.workbooks.open(r"C:\Users\XYZ\Desktop\Test\Summary.xlsx")
wb.UpdateLink()
wb.Save()
wb.Close()
xl_app.Quit()
I'm not sure if my solution solves your issue, but I had the same problem and I used LinkSources() and ChangeLink() instead
newSource = r"C:\Users\XYZ\Desktop\Test\XYZ_201606.csv"
oldSource = wb.LinkSources()
wb.ChangeLink(Name = oldSource[0], NewName = newSource, Type = 1)
Hope it helps!

Saving excel work book not working in python

for sheet_name in book.sheet_names():
for index in range(len(tabs)):
tab = tabs[index]
if sheet_name == tab:
dump_file_name = dump_files[index]
dump_file_name = file_prefix+dump_file_name
sheet = book.sheet_by_name(sheet_name)
new_book = Workbook()
sheet1 = new_book.add_sheet("Sheet 1")
for row in range(sheet.nrows):
values = []
for col in range(sheet.ncols):
sheet1.write(row,col,sheet.cell(row,col).value)
xlsx_file_name = dirname+"/"+dump_file_name+".xlsx"
sheet1.title = xlsx_file_name
new_book.save(xlsx_file_name)
The file is creating and data is there, but if I open it in openoffice.org and click the save button it asks for new name.
The file can not be read by PHP also. Again if I open and save it with new name then it works perfectly. I think we have to add something in the code so that it could be used by PHP.
i did google and found the solution here
http://xlsxwriter.readthedocs.org/getting_started.html
This is exactly what i wanted.
Creating and saving files to xlsx format.
Now its working perfectly.
original source
How to save Xlsxwriter file in certain path?
important link:
https://pypi.python.org/pypi/PyExcelerate

Categories