python_excel_openpyxl成功操作单元格位置变化并报数

该代码示例展示了如何使用openpyxl和xlrd库读取多个Excel文件,并将数据写入到财务状况表中。它分别打开资产负债表、利润表和纳税申报表,提取特定单元格数据,并根据条件判断写入财务状况表。此外,还计算了利润表中利息费用指标,根据正负数分别写入不同位置。最后,保存了更新后的财务状况表。
摘要由CSDN通过智能技术生成
from openpyxl import load_workbook


'''
@File    :
@Author  :  william
@Time    :  2020/09/29
@notice  :  null
@coding  : utf-8
'''
import xlrd
from xlutils.copy import copy
import openpyxl


#如下代码用于多个相关表的打开操作
fn_fuzhaibiao = '资产负债表.xlsx'
wb_fuzhaibiao = openpyxl.load_workbook(fn_fuzhaibiao)
ws_fuzhaibiao = wb_fuzhaibiao.get_sheet_by_name('资产负债表')

fn_lirunbiao = '利润表.xlsx'
wb_lirunbiao = openpyxl.load_workbook(fn_lirunbiao)
ws_lirunbiao = wb_lirunbiao.get_sheet_by_name('利润表')

rb_caiwuzhuangkuang = xlrd.open_workbook('财务状况表.xls', formatting_info=True)
wb_caiwuzhuangkuang = copy(rb_caiwuzhuangkuang)
ws_caiwuzhuangkuang = wb_caiwuzhuangkuang.get_sheet(0)


fn_nashuishenbaobiao = '纳税申报表.xlsx'
wb_nashuishenbaobiao = openpyxl.load_workbook(fn_nashuishenbaobiao)
ws_nashuishenbaobiao = wb_nashuishenbaobiao.get_sheet_by_name('Sheet1')



#如下代码用于将多个表中的相关数据写入财务状况表



all_sheets_fuzhaibiao = wb_fuzhaibiao.get_sheet_names()
print(all_sheets_fuzhaibiao)
sheet_fuzhaibiao = wb_fuzhaibiao.get_sheet_by_name(all_sheets_fuzhaibiao[0])
cunhuo_row=""
cunhuo_column=""
for row in sheet_fuzhaibiao.iter_rows():
    for cell in row:
        if str(cell.value).find("存货") != -1:
            cunhuo_row = cell.row
            cunhuo_column = cell.column


      #      print(str(cell.value).find("其中"))
            print(cell.row)
            print(int(cell.column)+3)
            print(cell.coordinate)


            print(sheet_fuzhaibiao.cell(row=13,column=1).value)

          #  print(cunhuo_column.value)
           # cunhuo_column=cell.column+3
          #  print(cell(row=cell.row,column=cunhuo_column).value)
            #cunhuo_coordinate=cell.coordinate+3
            #print(cunhuo_coordinate)
            #print(cell.column)









ws_caiwuzhuangkuang.write(2, 4, ws_fuzhaibiao['D13'].value)
ws_caiwuzhuangkuang.write(4, 4, ws_fuzhaibiao['C19'].value)
ws_caiwuzhuangkuang.write(5, 4, ws_fuzhaibiao['C8'].value)
ws_caiwuzhuangkuang.write(6, 4, ws_fuzhaibiao['C13'].value)
ws_caiwuzhuangkuang.write(7, 4, ws_fuzhaibiao['C23'].value)
ws_caiwuzhuangkuang.write(9, 4, ws_fuzhaibiao['C24'].value)
ws_caiwuzhuangkuang.write(10, 4, ws_fuzhaibiao['C35'].value)
ws_caiwuzhuangkuang.write(11, 4, ws_fuzhaibiao['G26'].value)


ws_caiwuzhuangkuang.write(13, 4, ws_lirunbiao['C4'].value)
ws_caiwuzhuangkuang.write(14, 4, ws_lirunbiao['C5'].value)
ws_caiwuzhuangkuang.write(15, 4, ws_lirunbiao['C6'].value)
ws_caiwuzhuangkuang.write(16, 4, ws_lirunbiao['C14'].value)
ws_caiwuzhuangkuang.write(22, 4, ws_lirunbiao['C24'].value)
ws_caiwuzhuangkuang.write(23, 4, ws_lirunbiao['C25'].value)
ws_caiwuzhuangkuang.write(24, 4, ws_lirunbiao['C27'].value)
ws_caiwuzhuangkuang.write(25, 4, ws_lirunbiao['C33'].value)
ws_caiwuzhuangkuang.write(26, 4, ws_lirunbiao['C34'].value)
#如下代码用于将多个表中的相关数据写入财务状况表------特例:利润表中利息费用指标,如果为正计财务状况表的利息收入,如果为负数计财务状况表的利息费用
if (ws_lirunbiao['C22'].value<0):
    ws_caiwuzhuangkuang.write(20, 4, abs(ws_lirunbiao['C22'].value))
else:
    ws_caiwuzhuangkuang.write(21, 4, ws_lirunbiao['C22'].value)


ws_caiwuzhuangkuang.write(27, 4, ws_nashuishenbaobiao['Z20'].value-ws_nashuishenbaobiao['Z21'].value+ws_nashuishenbaobiao['Z23'].value+ws_nashuishenbaobiao['Z24'].value+ws_nashuishenbaobiao['Z30'].value+ws_nashuishenbaobiao['Z31'].value-ws_nashuishenbaobiao['Z32'].value)


#保存财务状况表中修改的数据
wb_caiwuzhuangkuang.save('财务状况表.xls')
























使用openpyxl库可以读取单元格字体颜色,具体步骤如下: 1. 导入openpyxl库和颜色模块 ```python from openpyxl import load_workbook from openpyxl.styles import colors from openpyxl.styles import Font, Color ``` 2. 打开excel文件,获取工作簿和活动表单对象 ```python wb = load_workbook('example.xlsx') sheet = wb.active ``` 3. 循环遍历单元格,获取每个单元格的字体颜色 ```python for row in sheet.iter_rows(): for cell in row: font = cell.font color = font.color if isinstance(color, colors.Color): r, g, b = color.rgb print(f'The color of cell {cell.coordinate} is RGB({r}, {g}, {b})') else: print(f'The color of cell {cell.coordinate} is {color.theme}') ``` 上述代码中,我们使用了`iter_rows()`方法来遍历所有行,然后在每一行中遍历所有单元格。对于每个单元格,我们获取它的字体对象和颜色属性,并判断颜色是否为RGB格式。如果是,就打印出RGB值,否则打印出颜色的主题。 需要注意的是,如果单元格的字体颜色是自定义的颜色,则无法获取其RGB值,只能获取到颜色的主题。 完整代码示例: ```python from openpyxl import load_workbook from openpyxl.styles import colors from openpyxl.styles import Font, Color wb = load_workbook('example.xlsx') sheet = wb.active for row in sheet.iter_rows(): for cell in row: font = cell.font color = font.color if isinstance(color, colors.Color): r, g, b = color.rgb print(f'The color of cell {cell.coordinate} is RGB({r}, {g}, {b})') else: print(f'The color of cell {cell.coordinate} is {color.theme}') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值