2013-03-08 112 views
0

我有3個參數。用win32com和python擦除excel的一部分

STARTLINE,starColumn和寬度(此處2,8,3)

excel

我怎樣可以刪除無需在每個單元上寫入空白所選區域? (這裏只有30行,但有可能potetialy是10萬行)

現在我成功地計算行數,但我不能設法找到如何選擇和刪除區域

self.startLine = 2 
self.startColumn = 8 
self.width = 8 

self.xl = client.Dispatch("Excel.Application") 
self.xl.Visible = 1 
self.xl.ScreenUpdating = False 
self.worksheet = self.xl.Workbooks.Open("c:\test.xls") 
sheet = self.xl.Sheets("data") 

#Count the number of line of the record 
nb = 0 
while sheet.Cells(start_line + nb, self.startColumn).Value is not None: 
    nb += 1 

#must select from StartLine,startColumn to startcolum+width,nb 
#and then erase 

self.worksheet.Save() 

PS:代碼工作,我可能已經忘記了一些部分是由於做拷貝/ PAS錯誤,在現實中的Excel文件的處理是由幾個類互相

感謝繼承管理

+1

你能看到圖像嗎?它被阻止在我的網絡 – 2013-03-08 14:03:37

回答

1

什麼我通常做的是我記得ord宏在Excel中,並嘗試重新黑客在Python中的VB。對於刪除內容我有這樣的事情,不應該是很難將其轉換到Python:

Range("H5:J26").Select 
Selection.ClearContents 

在Python它應該是這樣的:

self.xl.Range("H5:J26").Select() 
self.xl.Selection.ClearContents() 

工作例如:

from win32com.client.gencache import EnsureDispatch 

exc = EnsureDispatch("Excel.Application") 
exc.Visible = 1 
exc.Workbooks.Open(r"f:\Python\Examples\test.xls") 
exc.Sheets("data").Select() 
exc.Range("H5:J26").Select() 
exc.Selection.ClearContents() 
+0

除了範圍不能適用於我的任何對象(self.xl或self.worksheet或工作表) – 2013-03-08 14:16:58

+0

好吧,讓我試試然後真正的:-)。 – Fenikso 2013-03-08 14:20:01

+0

增加了工作示例。 – Fenikso 2013-03-08 14:28:27

-1
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False): 
     res = super(res_users, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False) 
     if view_type == 'form': 
      model_obj = self.pool.get('ir.model.data') 
      tmp, target_view_id = model_obj.get_object_reference(cr, uid, 'base','view_users_form_simple_modif') 
      doc = etree.XML(res['arch']) 
      if view_id and view_id == target_view_id: 
       nodes = doc.xpath("//field[@name='context_warehouse']") 
       tmp, group_id = model_obj.get_object_reference(cr, uid, 'monos_base','group_can_change_context_warehouse') 
       cr.execute("select count(*) from res_groups_users_rel where uid = %s and gid = %s",(uid, group_id,)) 
       fetched = cr.fetchone()[0] 
       if fetched > 0: 
        for node in nodes: 
         node.set('readonly', '0') 
         node.set('modifiers', '{"required": true}') 
       else : 
        for node in nodes: 
         node.set('readonly', '1') 
         node.set('modifiers', '{"required": false,"readonly": true}') 
      nodes = doc.xpath("//field[@name='context_tz']") 
      for node in nodes: 
       node.set('readonly', '1') 
      res['arch'] = etree.tostring(doc) 
     return res 
+1

您是否嘗試回答其他問題? – bummi 2013-10-11 06:39:53

0

這工作對我來說

xl = EnsureDispatch('Excel.Application') 
wb2=xl.Workbooks.Open(file) 
ws=wb2.Worksheets("data") 

ws.Range("A12:B20").ClearContents()