python - 使用python添加两个列表和一个图像作为excel文件的标题

标签 python excel pandas xlsxwriter

我有两个列表,在 Excel 工作簿中有动态患者数据。因此,每个工作表都分配给一个患者。我想在 excel 中添加标题,以便在打印此工作簿时,在打印此标题时的每一页上都应显示。如图所示1 ,我希望标题部分 1#、标题部分 2# 和 Logo 成为标题。如何使用 xlsxwriter 在 python 中执行此操作?
RequiredHeader
我有这段代码,但如果用户在其中一个字段中输入长文本,它会移动我创建的静态标题。

    def write_header(self, index):
        first_index_copy = index
        # name
        if type(self.client['full_name'].unique()[0]) == str:
            full_name = self.client['full_name'].unique()[0]
        else:
            full_name = ''
        # birthday
        if type(self.client['birthday'].unique()[0]) == str:
            birthday = self.client['birthday'].unique()[0]
        else:
            birthday = ''
        # date of the log
        date_today = str(datetime.today().month) + "/" + str(datetime.today().year)
        # allergies
        allergies_str = ', '.join([elem for elem in self.client.allergy.unique() if str(elem) != 'nan'])
        header_list_part1 = self.map_dict.map_header(full_name, birthday, date_today, allergies_str)

        for i, line in enumerate(header_list_part1):
            self.mars.worksheet.write(index, 0, line[0], self.mars.bold_border)
            self.mars.worksheet.write(index, 1, line[1], self.mars.bold_border)
            index += 1

        header_list_part2 = self.map_dict.header_list_part2

        index += 2
        for i, elem in enumerate(header_list_part2):
            if i == len(header_list_part2) - 1:
                self.mars.worksheet.write(index, 0, elem, self.mars.bold)
            else:
                self.mars.worksheet.write(index, 0, elem)
            index += 1
        index = self.write_first_grid(index)
        self.mars.worksheet.insert_image('D'+str(first_index_copy+1), 'logo.jpg')

        return index
我还尝试了这段代码,它在每个工作表的顶部打印我所需的标题,但是当我打印它时,它只在一个地方给出,并且没有出现在该工作表的所有打印页面上,我理解这是因为如果 write我使用的函数而不是 set_header因此我的标题只是被视为普通文本而不是标题:
    def test_header(self, index):
        # name
        first_index_copy = index
        if type(self.client['full_name'].unique()[0]) == str:
            full_name = self.client['full_name'].unique()[0]
        else:
            full_name = ''
        # birthday
        if type(self.client['birthday'].unique()[0]) == str:
            birthday = self.client['birthday'].unique()[0]
        else:
            birthday = ''
        # date of the log
        date_today = str(datetime.today().month) + "/" + str(datetime.today().year)
        # allergies
        allergies_str = ', '.join([elem for elem in self.client.allergy.unique() if str(elem) != 'nan'])
        header_list_part1 = self.map_dict.map_header(full_name, birthday, date_today, allergies_str)
        for i, line in enumerate(header_list_part1):
            self.mars.worksheet.write(index, 0, line[0], self.mars.bold_border)
            self.mars.worksheet.write(index, 1, line[1], self.mars.bold_border)
            index += 1
        header_list_part2 = self.map_dict.header_list_part2

        index += 2
        for i, elem in enumerate(header_list_part2):
            if i == len(header_list_part2) - 1:
                self.mars.worksheet.write(index, 0, elem, self.mars.bold)
            else:
                self.mars.worksheet.write(index, 0, elem)
            index += 1
        index = self.write_first_grid(index)
        self.mars.worksheet.insert_image('D' + str(first_index_copy + 1),
                                         'logo.jpg')
你能帮我把它定义为一个标题,以便我在每个可打印的页面上看到它吗?

最佳答案

我能够通过以下方式做到这一点:

    def test_header(self):
        # name
        if type(self.client['full_name'].unique()[0]) == str:
            full_name = self.client['full_name'].unique()[0]
        else:
            full_name = ''
        # birthday
        if type(self.client['birthday'].unique()[0]) == str:
            birthday = self.client['birthday'].unique()[0]
        else:
            birthday = ''
        # date of the log
        date_today = str(datetime.today().month) + "/" + str(datetime.today().year)
        # allergies
        allergies_str = ', '.join([elem for elem in self.client.allergy.unique() if str(elem) != 'nan'])
        # creating patient info
        patient_data = 'Patient: ' + full_name +\
                       '\nDate of Birth: ' + birthday +\
                       '\nMonth/Year: ' + date_today +\
                       '\nAllergies: ' + allergies_str

        # creating header text which contains patient data and tenant image
        header_text = '&L&14&"Calibri,Bold"{}&R&G'.format(patient_data)
        self.mars.worksheet.set_header(header_text, {'image_right': 'logo.png'})
我希望它有用!

关于python - 使用python添加两个列表和一个图像作为excel文件的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62563556/

相关文章:

python - 比较元组列表中的多个项目并从同一列表中删除元组

python - 在给定索引处将一些字符串插入给定字符串

javascript - 使用 Python Flask_socketio react socket.io-client 不处理事件

vba - Excel VBA如何使用默认应用程序打开文件

python - 因为我持有十六进制格式的字符串而不对字符串进行编码?

excel - VBA 动态搜索模式与动态

excel - 如何根据 Option 和 Item Selected 从另一张工作表中加载单元格(价格)?

python - pandas - 获取由另一列索引的特定列的最新值(获取由另一列索引的特定列的最大值)

python - Pandas :如何计算组内的条件滚动/累积最大值

python 3.8.0 - 在新行上打印带有变量值的自记录表达式