Tag: win32com

python win32com excel中如何改变单元格中单个字符的颜色?

我有一个关于excel的win32com绑定的问题。 我设置了早期的绑定,并遵循O'Reilly的“Win32的Python编程”一书中的一些例子。 下面的代码工作正常: book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 1 book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 2 它根据编号改变整个单元的字体颜色。 但是这不起作用: book2.xlApp.Worksheets('Sheet1').Cells(1,1).Characters(start,length).Font.ColorIndex = 1 我得到以下callback: Traceback (most recent call last): File "<interactive input>", line 1, in <module> AttributeError: Characters instance has no __call__ method 但在Excels VBA中,代码起作用。 任何人都可以指出我的解决scheme? 我真的需要在Excel单元格中更改一部分string。 非常感谢你。

如何从Excel电子表格单元格中提取超链接?

我试图从Excel电子表格中的单个单元格中获取超链接,其代码如下: import win32com.client import win32ui app = win32com.client.Dispatch("Excel.Application") app.visible = True workbook = app.Workbooks.Open("test.xlsx") sheet = workbook.Sheets[0] test_cell = sheet.Range("A8").value 这将打印以下内容: test_cell u'Link title' 但是当我尝试提取超链接时,它没有以string格式返回链接/ url,而是返回一个'COMObject unknown': test_cell = sheet.Range("A8").Hyperlinks test_cell <COMObject <unknown>>

pythonwin32com excel边框格式

我在这里有一段代码,实际上是使用python win32com在Excel中格式化边框。 我关心的是格式化边界的时间。 我试图在excel中logging一个macros来找出所需的信息来转置它在我的脚本,但它没有工作。 所以我能做的最好的是运行在一个范围循环,我总是从第3行开始,直到一个叫做shn [1]的行计数器,增量为1,第1列为10,增量为1.从那里我使用“BorderAround()”工作正常,但太慢。 在这里我的一段代码: for shn in [("Beam-Beam", bb_row, bb_col), ("Beam-Col", bc_row, bc_col)]: sheet = book.Worksheets(shn[0]) sheet.Range( "J3:DW3" ).Copy() if shn[0] == "Beam-Col": sheet.Range( "J3:AA3" ).Copy() sheet.Range( sheet.Cells( 4, 10 ), sheet.Cells( shn[1]-1, 10 ) ).PasteSpecial() for mrow in xrange(3,shn[1],1): for mcol in xrange(1,10,1): sheet.Cells(mrow, mcol).BorderAround()#.Border(1) 有什么我可以做的格式的范围,如==> sheet.Range(sheet.Cells(3,1),sheet.Cells(shn [1],10))? 我试过“.Borders(11)”和“.Borders(12)”加“.BorderAround()”,但只有“.BorderAround()”已经工作。 提前致谢。

excel访问被win32 python pywin32拒绝

我的代码是 #Opens template for creating final report excel = win32.dynamic.Dispatch('Excel.Application') template = os.path.abspath((folderpath+'\Poop.xlsx')) wb = excel.Workbooks.Open(template) freshws= wb.Sheets("Fresh") #Sheet names must match perfectly secws= wb.Sheets("sec") cur.execute("Select * from FIRALL") freshdata=list(cur.fetchall()) #writes to the first sheet datarowlen=0 for i,a in enumerate(freshdata): datarowlen = len(a) for j,b in enumerate(a): freshws.Cells(i+1,j+1).Value = a[j] cur.execute("Select * from SECVE") secdata=list(cur.fetchall()) […]

win32com相当于xlrd的sheet.ncols

xlrd使得知道最后一列是什么非常容易。 有没有一个简单的方法使用win32com? 我曾尝试使用ws.UsedRange.Rows.Count,但这似乎并没有给出正确的答案。

如何等待,直到Excel继续使用win32com之前计算公式

我有一个win32com Python脚本,将多个Excel文件合并到一个电子表格中,并将其保存为PDF。 它现在的工作原理是输出几乎全是#NAME? 因为在计算Excel文件的内容(可能需要一分钟)之前输出该文件。 我如何强制工作簿计算值并等到完成之后再继续? excel = win32.Dispatch('Excel.Application') # moving stuff to this spreadsheet wb1 = excel.Workbooks.Open(filepath1) ws1 = excel.ActiveSheet # from this spreadsheet wb2 = excel.Workbooks.Open(filepath2) ws2 = excel.ActiveSheet # supposedly this should do it, but I haven't seen results ws1.EnableCalculation = True ws2.EnableCalculation = True ws1.Calculate ws2.Calculate wb1.Cells(2, 4).Value = wb2.Cells(1,1).Value # doing […]

用python将特殊值粘贴到另一个多个excel文件中

我试图使用glob两次,因此它可以复制并粘贴到多个文件。 但是这个东西只复制第一个数据,并且从第一个到最后一个打印所有的数据。 我想复制第一个不同的文件夹的第一个数据,然后第二个不同的文件夹第二个数据,依此类推。 提前感谢您的帮助 from win32com.client import Dispatch import glob ws = 'OUTPUT' ws2 = 'Original data' excel = Dispatch("Excel.Application") for filename in glob.glob("D:/apera/Workspace/Sounding/sounding*.xlsx"): data = excel.Workbooks.Open(filename) data.Worksheets(ws).Range('F1:H500').Copy() for filename2 in glob.glob("D:/apera/Workspace/Sounding2/sounding*.xlsx"): copy = excel.Workbooks.Open(filename2) copy.Worksheets(ws2).Range('A1:C500').PasteSpecial(Paste=-4163) 例 从sounding001_out复制值 然后在sounding001中粘贴特殊值 然后再试一次sounding_002到sounding002,依此类推

使用python和win32com取消Excel的closures事件

目前我尝试使用Python和win32com取消Excel的closures事件。 我已经设法在一个月前用IronPython来处理这个问题。 但为了我公司部门的进一步目的,这也应该能够与Python。 接下来你会看到两个片段。 第一个将包含IronPython代码 import clr clr.AddReference("Microsoft.Office.Interop.Excel") clr.AddReference("System.Windows.Forms") from Microsoft.Office.Interop import Excel from System.Windows.Forms import Form, Application, MessageBox, MessageBoxButtons, MessageBoxIcon, DialogResult class CloseEventTry(Form): def __init__(self): excel = Excel.ApplicationClass() excel.Visible = True excel.DisplayAlerts = False self.workbooks = excel.Workbooks.Add() self.Text = "Dummy GUI Window" #link "BeforeCloseEvent" to the "beforeClose" method self.workbooks.BeforeClose +=Excel.WorkbookEvents_BeforeCloseEventHandler(self.beforeClose) def beforeClose(self, cancel): print […]

Python的win32com与彭博插件打开Excel

我试图自动化build立一个使用Bloomberg插件来降低实时价格的Excel 2007电子表格。 问题是,当我通过win32com打开Excel的彭博插件不加载(所以所有的公式结束了“#NAME?”错误)。 手动卸载并重新安装插件的工作,但从logging的macros复制VBA代码导致“运行时错误13”:types不匹配“错误。 我可以点击结束button,一切运行良好,但我想要这个完全自动化。 我的代码是: import win32com.client xl = win32com.client.gencache.EnsureDispatch("Excel.Application") xl.Visible = True MainWorkBook = xl.Workbooks.Add(1) xl.AddIns("Bloomberg Excel Tools").Installed = False xl.AddIns("Bloomberg Excel Tools").Installed = True 设置DisplayAlerts = False不会捕获运行时错误。

Python Excel将macros运行到xlsx文件

我如何从我的xslm文件采取macros,并将其应用到我的xlsx文件? import cx_Oracle import xlsxwriter import win32com.client SQL = "Select * FROM TABLE" cursor1 = con.cursor() cursor1.execute(SQL) workbook = xlsxwriter.Workbook('OUTPUT.xlsx') worksheet = workbook.add_worksheet('Summary – Attendee') worksheet.set_tab_color('red') for i, row in enumerate(cursor1): for j, col in enumerate(row): worksheet.write(i+1,j,col) excel = win32com.client.Dispatch('Excel.Application') excel.Visible = 1 excel.Workbooks.Open(Filename="Path\Macro.xlsm") excel.Application.Run("Format") excel.Workbooks(1).Close(SaveChanges=1) excel.Application.Quit() excel = 0 workbook.close() Macro.xlsm VBA code: Sub […]